zoukankan      html  css  js  c++  java
  • 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);

    }

  • 相关阅读:
    SQL SERVER NVARCHAR字段INSERT 中文乱码问题解决
    解析$(this).data('type');
    使用HttpWebRequest发送自定义POST请求
    C#使用WebClient下载文件到本地目录
    linux开发时,想用的链接暂存
    程序进行重定位的技术按重定位的时机分类
    文件分类
    快表、页表
    操作系统的发展与分类
    网络操作系统和分布式操作系统的区别
  • 原文地址:https://www.cnblogs.com/chenqingwei/p/1651592.html
Copyright © 2011-2022 走看看