string.Split的问题

aimeast 2009-08-01 10:25:51
如下代码
    static void Main()
{
//11后面1个空格 22后面两个空格 33后面一个tab 44后面两个tab 55后面一个tab一个空格
string str = "11 22 33\t44\t\t55\t 66";
string[] sp = str.Split();
}

sp里面的结果是
		[0]	"11"	string
[1] "22" string
[2] "" string
[3] "33" string
[4] "44" string
[5] "" string
[6] "55" string
[7] "" string
[8] "66" string

为什么55后面只有一个空白?
这里是不是把tab和空格同样当做分隔符使用?
Split()是怎样运作的?
...全文
124 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
aimeast 2009-08-01
  • 打赏
  • 举报
回复
        string str = "11 22  33\t44\t \n\t55\t 66\n\t 77";
string[] sp = str.Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries);
aimeast 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sql77 的回复:]
C# codestring str="11 22 33\t44\t\t55\t 66";string[] sp= str.Split(newstring []{"","\t"},StringSplitOptions .RemoveEmptyEntries);foreach (string sin sp)
{
MessageBox.Show(s);
}
[/Quote]

谢谢,经改进,这个是最好的方案
string[] sp = str.Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries);
十八道胡同 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 aimeast 的回复:]
引用 5 楼 yulinlover 的回复:
str = str.Replace("2个空格","1个空格").replace("2个Tab","1个Tab")
string[] aryItems=str.Split(new char[]{'1个空格','1个Tab'})

我想这个方法并没有通用性
如果要分离的字符串里有多个tab,多个空格,多个tab和空格组合的情况,怎样处理?
[/Quote]
几个参数 StringSplitOptions.RemoveEmptyEntries即可
十八道胡同 2009-08-01
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
string str = "11 22 33\t44\t\t55\t 66";

string[] sp = str.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in sp)
{
Console.WriteLine(s);
}
}
// 11
//22
//33
//44
//55
//66
//请按任意键继续. . .
十八道胡同 2009-08-01
  • 打赏
  • 举报
回复
Split还有一个重载函数就是(new String[]{" ","\t"},StringSplitOptions .RemoveEmptyEntries)
这样后面必须有2个参数
aimeast 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yulinlover 的回复:]
str = str.Replace("2个空格","1个空格").replace("2个Tab","1个Tab")
string[] aryItems=str.Split(new char[]{'1个空格','1个Tab'})
[/Quote]
我想这个方法并没有通用性
如果要分离的字符串里有多个tab,多个空格,多个tab和空格组合的情况,怎样处理?
十八道胡同 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 aimeast 的回复:]
如果我想要的答案是11 22 33 44 55 66,这要怎样操作?
        [0]    "11"    string
        [1]    "22"    string
        [2]    "33"    string
        [3]    "44"    string
        [4]    "55"    string
        [5]    "66"    string

[/Quote]
你可以str.Split(new char[]{' ','\t'}) 前面那个是空格,后面Tab
SQL77 2009-08-01
  • 打赏
  • 举报
回复
            string str = "11 22  33\t44\t\t55\t 66";

string[] sp = str.Split(new string []{" ","\t"},StringSplitOptions .RemoveEmptyEntries);
foreach (string s in sp)
{
MessageBox.Show(s);
}
yulinlover 2009-08-01
  • 打赏
  • 举报
回复
str = str.Replace("2个空格","1个空格").replace("2个Tab","1个Tab")
string[] aryItems=str.Split(new char[]{'1个空格','1个Tab'})
aimeast 2009-08-01
  • 打赏
  • 举报
回复
如果我想要的答案是11 22 33 44 55 66,这要怎样操作?
[0] "11" string
[1] "22" string
[2] "33" string
[3] "44" string
[4] "55" string
[5] "66" string
dancingbit 2009-08-01
  • 打赏
  • 举报
回复
分隔符为空的话,默认使用空白字符,而制表符和回车换行符都和空格一样视为空白字符。
SQL77 2009-08-01
  • 打赏
  • 举报
回复
string[] sp = str.Split();
你这样默认是按空格分隔的吧
mythad 2009-08-01
  • 打赏
  • 举报
回复
使用正则System.Text.RegularExpressions.Regex.Split();

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧