更新时间:2023-02-03 08:24:54
如果它看起来难看,只是删除不必要的 ToCharArray
电话。
如果您想通过任一拆分 \\ n
或 \\ r
,你有两个选项:
If you want to split by either \n
or \r
, you've got two options:
使用数组文本 - 但是这会给你的空行的Windows风格的行尾 \\ r \\ n
:
var result = text.Split(new [] { '\r', '\n' });
使用一个普通的前pression,由Bart所示:
Use a regular expression, as indicated by Bart:
var result = Regex.Split(text, "\r\n|\r|\n");
如果您想preserve空行,你为什么要明确地告诉C#把它们扔掉? ( StringSplitOptions
参数) - 使用 StringSplitOptions.None
而不是
If you want to preserve empty lines, why do you explicitly tell C# to throw them away? (StringSplitOptions
parameter) – use StringSplitOptions.None
instead.