如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
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(); } } } |
以上这篇C# 字符串、数组和List的截取和转换实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/sanday/archive/2017/11/21/7873440.html