split的使用: 1、使用char()字元分隔:根據單個的char()類型的進行分隔 代碼如下: 2、利用多個字元來分隔字元串 代碼如下: 3、用字元串進行分隔 使用字元串進行分隔需要藉助C#的正則表達式,也就是需要使用Regex類。在Regex的Split方法中:第一個參數為需要分隔的字元串、第 ...
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(',')