C# 字符串、数组和List的截取和转换实例及两个list合并

一 . C# 字符串、数组和List的截取和转换实例

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
  
namespace ConsoleApp1
{
  class Program
  {
    /// <summary>
    /// 字符串,数组和List的截取,转换
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args)
    {
      string str = "abcdefghij1234567890";
      int i = 4;
      string str1 = str.Substring(0, i);                                   //截取字符串前i位--abcd
      string str2 = str.Remove(i, str.Length - i);                         //移除字符串i位后面的字符=截取字符串前i位--abcd
      string str3 = str.Remove(0, i);                                      //截取字符串i位之后的字符串--efghij1234567890
      string str4 = str.Substring(i);                                      //截取字符串i位之后的字符串--efghij1234567890
      string str5 = str.Substring(str.Length - i);                         //截取字符串后i位--7890
      string str6 = str.Remove(0, str.Length - i);                         //截取字符串后i位--7890
      string str7 = str.Substring(0, str.Length - i);             //去掉字符串后i位--abcdefghij123456
      string str8 = str.Remove(str.Length - i, i);               //去掉字符串后i位--abcdefghij123456
      string str9 = str.Replace("abc", "ABC");                  //替换字符串中的字符串--ABCdefghij1234567890
      string str0 = str.ToUpper();                         //小写字母转换成大写字母--ABCDEFGHIJ1234567890
      string str10 = str0.ToLower();                        //大写字母转换成小写字母--abcdefghij1234567890
      string str11= str.Substring(str.Length - 1, 1);              //截取字符串最后一位--0
      int m = str.IndexOf("cde") + 1;
      int n = str.IndexOf("23");
      string str12 = str.Substring(m, n - m + 2);               //截取从开始字符串到结束字符串范围--cdefghij123
      string s = "a,b,c,d,e,f,g,h,i,j";
      string[] strArray = s.Split(',');                      //字符串转数组
      string str13 = string.Join(",", strArray);                //数组转字符串
      List<string> list = new List<string>(s.Split(','));           //字符串转List
      string str14 = string.Join(",", list.ToArray());                     //List转字符串
      string[] str15 = list.ToArray();                                     //List转数组
      List<string> listS = new List<string>(str15);                        //数组转List
      Console.WriteLine(str0);
      Console.WriteLine(str12);
      Console.ReadLine();
    }
  }
    
}

 

二 . 两个集合合并

List<int> listA = new List<int> {1,2,3,5,7,9};
List<int> listB = new List<int> {13,4,17,29,2};
 
List<int> Result = listA.Union(listB).ToList<int>();           //剔除重复项
List<int> Result = listA.Concat(listB).ToList<int>();          //保留重复项
posted @ 2019-12-19 14:21  凌晨的星  阅读(1990)  评论(0编辑  收藏  举报