使用string.Split方法来分割字符串的注意事项:
string.Split给我们提供了非常灵活的使用方式, 但是如果使用不当, 会造成错误, 最近在做code review时, 看到大部分人这么使用:
strings = "A|B|:|C:D";
string[] ss = s.Split("|:|".ToCharArray());
//ss[0]: A
//ss[1]: B
//ss[2]:
//ss[3]:
//ss[4]: C
//ss[5]: D
其实他的本意是使用”|:|”将字符串分开的, 目标是为了得到数组: “A|B”和 “C:D”, 但是没有直接找到Split(string)的重载, 所有就是用了ToCharArray(), 很显然, 得到的结果是错误的.
正确的用法为:
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 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"} 保留空元素
个人理解:Split 就是和String.Join相反的一个函数 它的作用是把一个字符串分隔成含有多个字段的数组我现在只对上面的一种方法进行分析:string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"} 为什么返回的有了个空的“” 呢,自己当时也是不理解其实它就是以" , " 和“ . " 两个分隔符来拆分 字符串 1,2.3,,4的 当找到 3 后面的第一个”, “ 时候说明此处要分开了但是它后面又是一个分隔符 ”, " 怎么办呢 这是就只能当成 " " 空来处理了 你可以变相的把 1,2.3,,4连续的两个" , "间 认为有个空格符“ “也可以。不对的地方请大家帮指出来 不胜感激 。
LOFTER:hgfalgorithm http://hgfal.lofter.com/post/28eef2_e9e86a