标签:
String.Split()
用法为:
string[] ss1 = s.Split(new[] {"|:|"}, StringSplitOptions.None); //ss1[0]: A|B //ss1[1]: C:D后一个参数, 是标志是否自动remove empty的数据. 例如: "A|B|:|C:D|:|" 做分割时, 会返回三个数组, 该数组的最后一个元素是空"", 如果我们想过滤掉这些空的元素, 可以使用参数: StringSplitOptions.RemoveEmptyEntries 即: string[] ss1= s.Split(new[] {"|:|"}, StringSplitOptions.RemoveEmptyEntries);
s = "A|B|:|C:D|:|"; string[] ss1 = s.Split(new[] {"|:|"}, StringSplitOptions.RemoveEmptyEntries); //ss1[0]: "A|B" //ss1[1]: "C:D" ss1 = s.Split(new[] { "|:|" }, StringSplitOptions.None); //ss1[0]: "A|B" //ss1[1]: "C:D" //ss1[2]: ""
首先我们看下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[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"} string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"} string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素 string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素 string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
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"} 保留空元素
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"} 保留空元素
标签:
原文地址:http://blog.csdn.net/heyuchang666/article/details/51699840