拆分字符串有异常?

时间:2015-11-03 20:52:59

标签: c# arrays string split string-length

我有这样的字符串

string text = "224|3|0|0|0|0|0|0|0|0|0|0|0|0|2|You go back to meet the person from the Future.|1|You will be moved.|82|0|0|0|0|0|0|0|0|0|0|0|0|0|0|81|0|0|0|";

我怎么能分开“|”这个管道字符有异常,所以结果将是这样的3个部分?

22430000000000002
You go back to meet the person from the Future.|1|You will be moved.
820000000000000081000

我试过了:

string[] line = text.Split('|');
Console.Write(line[0]); //until line[15] For print section #1 

Console.Write(line[line.length-2]); //until line[line.length-20] For print section #3

文字是动态的,所以也许在第2节它会是

You go back to meet the person from the Future.|1|You will be moved.|2|Hello

因此,当索引达到[16]时,文本才会被分割,直到line.length-20 这可能吗 ? 谢谢!!

1 个答案:

答案 0 :(得分:3)

有点做作,但假设您想要将内部字母与外部数字分开工作:

string text = "224|3|0|0|0|0|0|0|0|0|0|0|0|0|2|You go back the person from the Future.|1|You will be moved.|82|0|0|0|0|0|0|0|0|0|0|0|0|0|0|81|0|0|0|";

var first = String.Join("", text.TakeWhile(c => Char.IsDigit(c) || c == '|'));

var third = String.Join("", text.Reverse().TakeWhile(c => Char.IsDigit(c) || c == '|').Reverse());

var second = text.Replace(first, "").Replace(third, "");