c# - 分割字符串有异常吗?

标签 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 这可能吗 ? 谢谢!!

最佳答案

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

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/

相关文章:

java - 为 Java 初学者复制字符串 n 次

python - 将 numpy 数组添加到堆队列

javascript - 如何从 JSON 对象填充列表并创建 TreeView - JavaScript

C#排列数组列表?

c# - 是否可以在继承类中覆盖抽象父类的嵌套类定义?

java - 我可以在将对象与另一个数组进行比较时更改对象的一个​​值吗?

c++ - 使用 boost 抛出异常的 Base64 编码

Java 字符串外部化键缺失

c# - 在 C# 中用复杂的语法解析逗号分隔的字符串

c# - 在 WebClient 中接受 Cookies?