我有这样的字符串
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 这可能吗 ? 谢谢!!
最佳答案
有点做作,但假设您想将内部字母与外部数字分开:
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, "");
关于c# - 分割字符串有异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33508861/