• List<string>常用操作


    1.定义变量

    IEnumerable<string> lstNew = null;

    List<string> lstOne = new List<string>(){"January", "February", "March"};

    List<string> lstTwo = new List<string>() { "January", "April", "March"};

    List<string> lstThree = new List<string>() { "January", "April", "March", "May" };

    List<string> lstFour = new List<string>() { "Jan", "Feb", "Jan", "April", "Feb" };

    2.输入

    static void PrintList(IEnumerable<string> str)

    {

    foreach (var s in str)

    Console.WriteLine(s);

    Console.WriteLine("-------------");

    }

    3.常用操作(命名空间:System.Linq)

          Enumberable.Intersect();

         // Compare two List<string> and display common elements

    lstNew = lstOne.Intersect(lstTwo, StringComparer.OrdinalIgnoreCase);

    PrintList(lstNew);

    输出相同元素:JANUARY MARCH

    // Compare two List<string> and display items of lstOne not in lstTwo

    lstNew = lstOne.Except(lstTwo, StringComparer.OrdinalIgnoreCase);

    PrintList(lstNew);

    // Unique List<string>

    lstNew = lstFour.Distinct(StringComparer.OrdinalIgnoreCase);

    PrintList(lstNew)

    // Convert elements of List<string> to Upper Case

    lstNew = lstOne.ConvertAll(x => x.ToUpper());

    PrintList(lstNew);

    // Concatenate and Sort two List<string>

    lstNew = lstOne.Concat(lstTwo).OrderBy(s => s);

    PrintList(lstNew);

    // Concatenate Unique Elements of two List<string>

    lstNew = lstOne.Concat(lstTwo).Distinct();

    PrintList(lstNew);

    // Reverse a List<string>

    lstOne.Reverse();

    PrintList(lstOne);

    // Search a List<string> and Remove the Search Item

    // from the List<string>

    int cnt = lstFour.RemoveAll(x => x.Contains("Feb"));

    Console.WriteLine("{0} items removed", cnt);

    PrintList(lstFour);

    // Order by Length then by words (descending)

    lstNew = lstThree.OrderBy(x => x.Length)

            .ThenByDescending(x => x);

    PrintList(lstNew);

    C#

    // Create a string by combining all words of a List<string>

    // Use StringBuilder if you want performance

    string delim = ",";

    var str = lstOne.Aggregate((x, y) => x + delim + y);

    Console.WriteLine(str);

    Console.WriteLine("-------------");

    C#

    // Create a List<string> from a Delimited string

    string s = "January February March";

    char separator = ' ';

    lstNew = s.Split(separator).ToList();

    PrintList(lstNew);

    Use the List(T).ConvertAll method

    C#

    // Convert a List<int> to List<string>

    List<int> lstNum = new List<int>(new int[] { 3, 6, 7, 9 });

    lstNew = lstNum.ConvertAll<string>(delegate(int i)

    {

    return i.ToString();

    });

    PrintList(lstNew);

    // Count Repeated Words

    var q = lstFour.GroupBy(x => x)

       .Select(g => new { Value = g.Key, Count = g.Count() })

       .OrderByDescending(x => x.Count);

    foreach (var x in q)

    {

         Console.WriteLine("Value: " + x.Value + " Count: " + x.Count);

    }

  • 相关阅读:
    html概述
    html元素之meta
    ICMP
    https
    html元素之link
    数据链路层
    UDP
    js概述
    html元素之base
    vagrant和virtualbox无法创建软连接(failed to create symbolic link : Protocol error)
  • 原文地址:https://www.cnblogs.com/chenqingwei/p/1651592.html
Copyright © 2020-2023  润新知