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 var intersectedList = list.Intersect(listS);//两个List交集 var expectedList = list.Except(listS);//两个List取差 var mergedList = list.Merge(listS);//两个List取联集 var result = list.Union(listS).ToList<int>(); //除去重复 var result1 = list.Concat(listS).ToList<int>();//保留重复 var result2 = list.BinarySearch("1");//判断集合中是否包含某个值.如果包含则返回0 Console.WriteLine(str0); Console.WriteLine(str12); Console.ReadLine(); } } }