C# 怎样分离字符串
string hah= "123456789 abcdefgh| "(中间有好几个空格)
string[] strs = hah.Split( '| ');
for(int i=0;i <strs.Length-1;i++)
{
hah1=strs[i].ToString();
string[] strs1 = hah1.Split( ' ');
textBox1.AppendText(strs1[0]);
textBox1.AppendText(strs1[1]);
}
我想把hah字符分离开来变成 "123456789 "和 "abcdefgh "可是上面的代码不行啊。。。大家帮帮忙!
------解决方案--------------------http://topic.csdn.net/t/20060812/14/4945004.html
这里有答案
------解决方案--------------------string hah = "123456789 abcdefgh| ";
string[] strs = hah.Split( '| ');
for (int i = 0; i < strs.Length - 1; i++)
{
string hah1 = strs[i].ToString();
string[] strs1 = hah1.Split( ' ');
textBox1.Text = (strs1[0]);
textBox2.Text = (strs1[strs1.Length -1]);
------解决方案--------------------先把空格给替换成单个空格,在SPlit
------解决方案--------------------textBox1.AppendText(strs1[0]);
textBox1.AppendText(strs1[1]);
改为:
textBox1.appendText(strs1[0]);
textBox1.AppendText(strs1[strs1.length-1]);
------解决方案--------------------string hah = "123456789 abcdefgh| ";
string[] result = System.Text.RegularExpressions.Regex.Split(hah, @ "\s+|\| ");
foreach(string str in result)
{
if (!string.IsNullOrEmpty(str)) //字符串非空
Response.Write(str + " <br /> "); //分离出来的字符串
}