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() "
");
输出结果:
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() "
");
输出结果:
aaa
bbb
ccc
ddd
eee
3、用单个字符来分隔:
string str="aaajbbbjccc";
string[] sarray=str.split('j');
foreach(string i in sarray) response.write(i.tostring() "
");
输出结果:
aaa
bbb
ccc
////////////////////////////////////////////////
string[] arr = str.split("o");
这是一个具有语法错误的语句,split 的 separator 参数应该是 [] 或 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(',')
阅读(2167) | 评论(0) | 转发(0) |