split的使用:
1、使用char()字符分隔:根据单个的char()类型的进行分隔
代码如下:
string str="e2kdk2fjod2fiksf21";
string [] sarray=str.split('2');//因为是用char类型分隔字符,使用一定要用单引号
//sarray的值:sarray[0]="e",sarray[1]="kdk",sarray[2]="fjod",sarray[3]="fiksf",sarray[4]="1";
2、利用多个字符来分隔字符串
代码如下:
string str= "tgedtc_guizhisheng_guiyhi";
string [] sarray=str.split(new char[] {'j','g' });//等同于str.split(new char[2] {'j','g' })
//sarray值:sarray[0]="t",sarray[1]="edtc_",sarray[2]="uizhishen",sarray[3]="_",sarray[4]="uiyhi"
3、用字符串进行分隔
使用字符串进行分隔需要借助c#的正则表达式,也就是需要使用regex类。在regex的split方法中:第一个参数为需要分隔的字符串、第二个参数是分隔标识的正则表达式、第三个参数是可选参数。
代码如下:
string str= "tgedtc_guizhisheng_guiyhi";
string [] sarray=regex.split(str, "ui");
//sarray值:sarray[0]="tgedtc_g",sarray[1]="zhisheng_g",sarray[2]="yhi"
4、利用字符串或字符串组进行分隔
代码如下:
string str= "tgedtc_guizhisheng_gui uiyhi";
//单个字符串
string [] sarray=str.split(new string[] { "ui" }, stringsplitoptions.removeemptyentries);
//sarray值:sarray[0]="tgedtc_g",sarray[1]="zhisheng_g",sarray[2]=" ",sarray[3]="yhi"
string.split 方法有6个重载函数:
程序代码
1) public string[] split(params char[] separator)
2) public string[] split(char[] separator, int count)
3) public string[] split(char[] separator, stringsplitoptions options)
4) public string[] split(string[] separator, stringsplitoptions options)
5) public string[] split(char[] separator, int count, stringsplitoptions options)
6) public string[] split(string[] separator, int count, stringsplitoptions options)
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
1. public string[] split(params char[] separator)
程序代码
string[] split = words.split(new char[] { ',' });//返回:{"1","2.3","","4"}
string[] split = words.split(new char[] { ',', '.' });//返回:{"1","2","3","","4"}
2. public string[] split(char[] separator, int count)
程序代码
string[] split = words.split(new char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
string[] split = words.split(new char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
3. public string[] split(char[] separator, stringsplitoptions options)
程序代码
string[] split = words.split(new char[] { ',', '.' }, stringsplitoptions.removeemptyentries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.split(new char[] { ',', '.' }, stringsplitoptions.none);//返回:{"1","2","3","","4"} 保留空元素
4. public string[] split(string[] separator, stringsplitoptions options)
程序代码
string[] split = words.split(new string[] { ",", "." }, stringsplitoptions.removeemptyentries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.split(new string[] { ",", "." }, stringsplitoptions.none);//返回:{"1","2","3","","4"} 保留空元素
5. public string[] split(char[] separator, int count, stringsplitoptions options)
程序代码
string[] split = words.split(new char[] { ',', '.' }, 2, stringsplitoptions.removeemptyentries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.split(new char[] { ',', '.' }, 6, stringsplitoptions.none);//返回:{"1","2","3","","4"} 保留空元素
6. public string[] split(string[] separator, int count, stringsplitoptions options)
程序代码
string[] split = words.split(new string[] { ",", "." }, 2, stringsplitoptions.removeemptyentries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.split(new string[] { ",", "." }, 6, stringsplitoptions.none);//返回:{"1","2","3","","4"} 保留空元素
需要注意的是没有重载函数public string[] split(string[] separator),所以我们不能像vb.net那样使用words.split(","),而只能使用words.split(',')
如对本文有疑问,
点击进行留言回复!!
网友评论