如果我有:
Some text
More text
Even more text
Run Code Online (Sandbox Code Playgroud)
获得更优雅的方式是什么:
Some text
More text
Even more text
Run Code Online (Sandbox Code Playgroud)
所有人都知道重复令牌的数量
使用正则表达式的方法是
string replaced = System.Text.RegularExpressions.Regex
.Replace(input, @"(?:\r\n)+", "\r\n");
Run Code Online (Sandbox Code Playgroud)
((?:...)语法是一个非捕获组,可以用捕获组替换(只是(...)),但效率稍差且不易读,IMO.)
也许是这样的:
var result = string.Join("\r\n", s.Split(new[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries))
Run Code Online (Sandbox Code Playgroud)