String.Split()函数多种使⽤实例
我们在上次学习到了 String.Join函数(),其中⽤到了String.SPlit函数,所以可以上⽹查了该函数的使⽤⽅法如下:
#中使⽤string.Split⽅法来分割字符串的注意事项:
string.Split给我们提供了⾮常灵活的使⽤⽅式, 但是如果使⽤不当, 会造成错误, 最近在做code review时, 看到⼤部分⼈这么使⽤:
string s = "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)
程序代码
c++中string的用法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连续的两个" , "间认为有个空格符“ “也可以。不对的地⽅请⼤家帮指出来不胜感激。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。