split的3种方法
方法1: 用单个字符来分隔
string str = "aaa,bbb,ccc";
//得到逗号分隔的各个字符
string[] sArray = str.Split(',');
//string[] sArray = str.Split(new char[]{','}); //这种写法也可以
foreach (string i in sArray)
{
Response.Write(i.ToString() + "<br>");
}
//得到逗号分隔的各个字符
string[] sArray = str.Split(',');
//string[] sArray = str.Split(new char[]{','}); //这种写法也可以
foreach (string i in sArray)
{
Response.Write(i.ToString() + "<br>");
}
输出结果:
aaa
bbb
ccc
bbb
ccc
方法2:用多个字符来分隔
string str = "aaajbbbscccjdddseee";
//得到以'j'或's'分隔的各个字符串
string[] sArray = str.Split(new char[] {'j', 's'});
foreach (string i in sArray)
{
Response.Write(i.ToString() + "<br>");
}
//得到以'j'或's'分隔的各个字符串
string[] sArray = str.Split(new char[] {'j', 's'});
foreach (string i in sArray)
{
Response.Write(i.ToString() + "<br>");
}
输出结果:
aaa
bbb
ccc
ddd
eee
bbb
ccc
ddd
eee
方法3: 用字符串分隔
using System.Text.RegularExpressions;
string str = "aaajsbbbjsccc";
//得到以'js'分隔的各个字符串
string[] sArray = Regex.Split(str, "js", RegexOptions.IgnoreCase);
foreach (string i in sArray)
{
Reponse.Write(i.ToString() + "<br>");
}
string str = "aaajsbbbjsccc";
//得到以'js'分隔的各个字符串
string[] sArray = Regex.Split(str, "js", RegexOptions.IgnoreCase);
foreach (string i in sArray)
{
Reponse.Write(i.ToString() + "<br>");
}
输出结果:
aaa
bbb
ccc
bbb
ccc