标签:one bbb 多个 示例 重载函数 ons 使用 foreach new
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
这是一个具有语法错误的语句,Split 的 separator 参数应该是 char[] 或 string[],不应是字符串。正确的示例:
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[] split = words.Split(new Char[] { ‘,‘ });//返回:{"1","2.3","","4"} string[] split = words.Split(new Char[] { ‘,‘, ‘.‘ });//返回:{"1","2","3","","4"}
string[] split = words.Split(new Char[] { ‘,‘, ‘.‘ }, 2);//返回:{"1","2.3,,4"} string[] split = words.Split(new Char[] { ‘,‘, ‘.‘ }, 6);//返回:{"1","2","3","","4"}
string[] split = words.Split(new Char[] { ‘,‘, ‘.‘ }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素 string[] split = words.Split(new Char[] { ‘,‘, ‘.‘ }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素 string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
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"} 保留空元素
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(‘,‘)
标签:one bbb 多个 示例 重载函数 ons 使用 foreach new
原文地址:http://www.cnblogs.com/wangfuyou/p/6101992.html