C# 字符串、数组和List的截取和转换实例
程序员文章站
2023-12-18 15:17:10
如下所示:
using system;
using system.collections.generic;
using system.diagnostics;...
如下所示:
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的截取和转换实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。