首页 新闻 会员 周边 捐助

C#的split的用法

0
[已解决问题] 解决于 2013-11-09 21:31

C#的split的用法具体是怎么的,最好详细一点。

C#
冰花ぃ雪魄的主页 冰花ぃ雪魄 | 菜鸟二级 | 园豆:210
提问于:2013-11-08 12:51
< >
分享
最佳答案
1
1、用字符串分隔: 
using System.Text.RegularExpressions;
string str="aaajsbbbjsccc";
string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);
foreach (string i in sArray) Response.Write(i.ToString() + "<br>");
输出结果:
aaa
bbb
ccc
2、用多个字符来分隔:
string str="aaajbbbscccjdddseee"; 
string[] sArray=str.Split(new char[2] {'j','s'}); 
foreach(string i in sArray) Response.Write(i.ToString() + "<br>"); 
输出结果:
aaa
bbb
ccc
ddd
eee
3、用单个字符来分隔:
string str="aaajbbbjccc";
string[] sArray=str.Split('j');
foreach(string i in sArray) Response.Write(i.ToString() + "<br>");
输出结果:
aaa
bbb
ccc

 

 
////////////////////////////////////////////////
string[] arr = str.Split("o");

这是一个具有语法错误的语句,Split 的 separator 参数应该是 char[] 或 string[],不应是字符串。正确的示例:

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(',')
冰花ぃ雪魄 | 菜鸟二级 |园豆:210 | 2013-11-08 12:51
其他回答(1)
0

string.split()方法是用来按照某个或某些(数组)字符或字符串来进行分割的,可以把一些不想要的字符通过这种方式给移除掉,具体用法楼上已经讲的很详细了。

轻狂の书生 | 园豆:1042 (小虾三级) | 2013-11-09 18:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册