[点晴永久免费OA]C#中Split用法(把字符串以某个字符为分隔符分成一个数组)
当前位置:点晴教程→点晴OA办公管理信息系统
→『 经验分享&问题答疑 』
1、用字符串分隔: string[] foreach 输出结果: aaa bbb ccc 2、用多个字符来分隔: string string[] 输出结果: aaa bbb ccc ddd eee string string[] foreach(string 输出结果: aaa bbb ccc //////////////////////////////////////////////// string[] arr = str.Split("o"); 这是一个具有语法错误的语句,Split 的 separator 参数应该是 string str = "technology"; char[] separator = { ''o'' }; string[] arr = str.Split(separator); //////////////////////////////////////////////////// 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('','') 该文章在 2017/2/24 15:29:00 编辑过 |
关键字查询
相关文章
正在查询... |