.net c# split 用子字符串分割字符串
string str="select * from tree where treetop=1";
目的:要将str用"where"分割
错误做法:
string
[] s
=
str.split(
new
char
[
5
]
...
{'w','h','e','r','e'}
);
string tmp = "" ;
foreach ( string ss in s)
tmp += " * " + ss.ToString() + " # " ;
Label1.Text = tmp;
string tmp = "" ;
foreach ( string ss in s)
tmp += " * " + ss.ToString() + " # " ;
Label1.Text = tmp;
结果:
*s#*l#*ct * f#*om t#*#*#* #*#*#*#*#* t#*#*#*top=1#
正确做法:结果:
string
[] s
=
str.Split(
new
string
[
1
]
...
{ "where" }
, System.StringSplitOptions.None);
string tmp = "" ;
foreach ( string ss in s)
tmp += " * " + ss.ToString() + " # " ;
Label1.Text = tmp;
string tmp = "" ;
foreach ( string ss in s)
tmp += " * " + ss.ToString() + " # " ;
Label1.Text = tmp;
*select * from tree #* treetop=1#
还有一种方法是可行的,就是正则表达式。
using
System.Text.RegularExpressions;
protected void Page_Load( object sender, EventArgs e)
... {
string content="agcsmallmacsmallgggsmallytx";
string[] resultString = Regex.Split(content, "small", RegexOptions.IgnoreCase);
content="";
foreach (string i in resultString)
content += i+" ";
Label1.Text =content;
}
protected void Page_Load( object sender, EventArgs e)
... {
string content="agcsmallmacsmallgggsmallytx";
string[] resultString = Regex.Split(content, "small", RegexOptions.IgnoreCase);
content="";
foreach (string i in resultString)
content += i+" ";
Label1.Text =content;
}
输出"agc mac ggg ytx"