且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

询问:将多个新行替换为单个新行或br

更新时间:2023-12-03 17:28:28

使用数组
String.Join(System.Environment.NewLine, text.Split(System.Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))



或正则表达式



or regular expression

System.Text.RegularExpressions.Regex.Replace(System.text, "[\\r\\n]+", System.Environment.NewLine, System.Text.RegularExpressions.RegexOptions.Multiline)



或最后循环



or loop at last

while (txt.Contains(System.Environment.NewLine + System.Environment.NewLine)) {
	txt = txt.Replace(System.Environment.NewLine + System.Environment.NewLine, System.Environment.NewLine);
}


获得Regex的帮助.下面的代码为我工作:
Take help of Regex. Below code worked for me:
textBox1.Text = Regex.Replace(textBox1.Text, "[\r\n]+", "\r\n");



希望这也对您有用.



Hope this works for you as well.