1墨榄、用字符串分隔(正則):
using System.Text.RegularExpressions;
string str="aaajsbbbjsccc";
string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);
結(jié)果:
aaa
bbb
ccc
2、用多個字符來分隔:
string str="aaajbbbscccjdddseee";
string[] sArray=str.Split(new char[2] {'j','s'});
結(jié)果:
aaa
bbb
ccc
ddd
eee
3藐吮、用單個字符來分隔:
string s=abcdeabcdeabcde;
string[] sArray=s.Split('c') ;
結(jié)果:
ab
deab
deab
de
Split 的 separator 參數(shù)應(yīng)該是char[] 或 string[]剑辫,不應(yīng)是字符串哭尝。
Split分隔字符串時驹吮,如果分隔符為一個字符,一般情況下返回的字符數(shù)組長度是預(yù)期的贡定。
但當(dāng)分隔符為多個字符時赋访,比如 str.Split("|||".ToCharArray()) ,返回的字符數(shù)組中可能會多出預(yù)期的長度缓待,數(shù)組中會出現(xiàn)一些值為空的字符串元素蚓耽。
可以采用正則表達(dá)式來分割:
string[] arr = Regex.Split( str, @"|||" ,RegexOptions.IgnoreCase );
注意:| 在正則表達(dá)式中是保留字符,需要用""轉(zhuǎn)義旋炒。
分割文本域中以回車換行的例子:
string[] arr = s.Split(new string[] { "\r\n" }, StringSplitOptions.None)
string[] arr = Regex.Split( str , "\r\n" ,RegexOptions.IgnoreCase)
String.Split 方法有6個重載函數(shù):
程序代碼
- public string[] Split(params char[] separator)
- public string[] Split(char[] separator, int count)
- public string[] Split(char[] separator, StringSplitOptions options)
- public string[] Split(string[] separator, StringSplitOptions options)
- public string[] Split(char[] separator, int count, StringSplitOptions options)
- public string[] Split(string[] separator, int count, StringSplitOptions options)
下邊我們通過一些實例來說明下怎么使用(以下string words = "1,2.3,,4";):
- 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"}
- public string[] Split(params char[] separator)
- 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"}
- public string[] Split(char[] separator, int count)
- public string[] Split(char[] separator, StringSplitOptions options)
程序代碼
//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);
//保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"}
- public string[] Split(char[] separator, StringSplitOptions options)
- public string[] Split(string[] separator, StringSplitOptions options)
程序代碼
//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);
//保留空元素
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"}
- public string[] Split(string[] separator, StringSplitOptions options)
- public string[] Split(char[] separator, int count, StringSplitOptions options)
程序代碼
//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);
//保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"}
- public string[] Split(char[] separator, int count, StringSplitOptions options)
- public string[] Split(string[] separator, int count, StringSplitOptions options)
程序代碼
//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);
保留空元素
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"}
- public string[] Split(string[] separator, int count, StringSplitOptions options)