关于字符串分隔的问题。Split

banxiankin 2009-06-05 10:20:54
class TestStringSplit
{
static void Main()
{
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };

string text = "one\ttwo three:four,five six seven";
System.Console.WriteLine("Original text: '{0}'", text);

string[] words = text.Split(delimiterChars);
System.Console.WriteLine("{0} words in text:", words.Length);

foreach (string s in words)
{
System.Console.WriteLine(s);
}
}
}
分隔每个数组是 word[0] = 'one' word[1] = 'two' word[2] = 'three' ....
我想要结果是 word[0]= 'one' word[1] = '\t' word[2] = 'two' word[3] = 'three'...
如何实现。
...全文
31 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
banxiankin 2009-06-08
  • 打赏
  • 举报
回复
结贴了,谢谢大家。
banxiankin 2009-06-08
  • 打赏
  • 举报
回复
哈哈,问了我们公司的一个兼职。给了我一个超级简单的方法。没有用什么正则。

1,先把分隔符的两端加上特殊字符,保证你字符串中不会出现的字符。比如¥。 “,”替换成 “¥,¥”
2,用split函数,分隔符为¥。就可以把你要的字符串和原来的分隔符一起放入数组了。
流年 2009-06-05
  • 打赏
  • 举报
回复
用正则应该能实现,以前做过类似的,但是正则不熟
gciyfzx07 2009-06-05
  • 打赏
  • 举报
回复
路过
yys777 2009-06-05
  • 打赏
  • 举报
回复
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };

string text = "one\ttwo three:four,five six seven";
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
HashSet<string> hs = new HashSet<string>();
bool b1 = false;
bool b2 = false;
foreach (char C in text)
{
if ((C >= '\x61' && C <= '\x7A') || (C >= '\x41' && C <= '\x5A'))
{
if (!b1)
{
if (sb2.Length > 0)
{
hs.Add(sb2.ToString());
}
MessageBox.Show(sb2.ToString());
sb1 = new StringBuilder();
sb2 = new StringBuilder();
}
b1 = true;
b2 = false;
sb1.Append(C.ToString());
}
else
{
if (!b2)
{
if (sb1.Length > 0)
{
hs.Add(sb1.ToString());
}
MessageBox.Show(sb1.ToString());
sb2 = new StringBuilder();
sb1 = new StringBuilder();
}
b1 = false;
b2 = true;
sb2.Append(C.ToString());
}
呵呵,办法比较笨
banxiankin 2009-06-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 sq_zhuyi 的回复:]
string str = "one\ttwo three:four,five six seven";
List <string> list = new List <string>();
Regex reg = new Regex(@"\w");
Match mat = reg.Match(str);
while(mat.Success)
{
list.Add(mat.Value);
mat = reg.Match(str, mat.Index+mat.Length);
}
[/Quote]
还在研究正则,不过这段代码我运行了。还是没出来我想要的结果。
banxiankin 2009-06-05
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 crazyleo814 的回复:]
class TestStringSplit
{
static void Main()
{
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };

string text = "one\ttwo three:four,five six seven";
System.Console.WriteLine("Original text: '{0}'", text);

string[] words = text.Split(delimiterChars);
System.Console.WriteLine("{0} words in text:", words.Length);

foreac…
[/Quote]
不行,字符串里面不一定有多少个空格回车等分隔符。所以无法判断。
crazyleo814 2009-06-05
  • 打赏
  • 举报
回复
class TestStringSplit
{
static void Main()
{
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };

string text = "one\ttwo three:four,five six seven";
System.Console.WriteLine("Original text: '{0}'", text);

string[] words = text.Split(delimiterChars);
System.Console.WriteLine("{0} words in text:", words.Length);

foreach (string s in words)
{
System.Console.WriteLine(s);
}
}
}
分隔每个数组是 word[0] = 'one' word[1] = 'two' word[2] = 'three' ....
word[0]= 'one' word[1] = '\t' word[2] = 'two' word[3] = ' ' word[4] = 'three'...
如何实现。


你的那个char[]可以取到里边的值
判断单数
将值按顺序插入到word[单数]="char值"
mbh0210 2009-06-05
  • 打赏
  • 举报
回复
对text 先处理,换行符号或者空格替换掉,这样在用分割符
ximi82878 2009-06-05
  • 打赏
  • 举报
回复
正则不熟,不知道帮顶给不给分
MOmo400 2009-06-05
  • 打赏
  • 举报
回复

string text = "one\ttwo three:four,five six seven";

Regex r = new Regex("\t| |:|,");
String[] temp = r.Split(text);
路人乙e 2009-06-05
  • 打赏
  • 举报
回复
Regex reg = new Regex(@"(\w)|(\s)");
dancingbit 2009-06-05
  • 打赏
  • 举报
回复
没有,自己做个实现吧。
SuperTyro 2009-06-05
  • 打赏
  • 举报
回复
用正则拆吧
路人乙e 2009-06-05
  • 打赏
  • 举报
回复
string str = "one\ttwo three:four,five six seven";
List<string> list = new List<string>();
Regex reg = new Regex(@"\w");
Match mat = reg.Match(str);
while(mat.Success)
{
list.Add(mat.Value);
mat = reg.Match(str, mat.Index+mat.Length);
}
banxiankin 2009-06-05
  • 打赏
  • 举报
回复
恩。我知道,split不包含分隔符。所以求个方法,简单的实现我要的效果。c#有没有现成的方法。
刚刚开始学c#。谢谢了。
jiyan1221 2009-06-05
  • 打赏
  • 举报
回复
这个帮顶只能。。
LZ的方法肯定不行,要变通。
dancingbit 2009-06-05
  • 打赏
  • 举报
回复
Split的结果中是不会包含分隔符的。
banxiankin 2009-06-05
  • 打赏
  • 举报
回复
说白了,我就是想按照顺序,把分隔符也放入数组。
banxiankin 2009-06-05
  • 打赏
  • 举报
回复
更正 我想要结果是 word[0]= 'one' word[1] = '\t' word[2] = 'two' word[3] = ' ' word[4] = 'three'...

110,538

社区成员

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

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

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