询问:将多个新行替换为单个新行或br [英] Ask: Replace multiple new line into single new line or br
本文介绍了询问:将多个新行替换为单个新行或br的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好..
我的替换字符有问题.
如果我有一个文本框,并且我用许多新行将其写入文本框.
结果是我希望将许多新行替换为一两个新行,例如facebook中的注释.
请您的协助和您的意见.
谢谢
Ade
hello..
i have problem with replace character.
if i have one textbox, and i write into textbox with many new line.
and the results I wanted a lot of new lines replaced with one or two new lines such as comments in the facebook.
please your assistance and your opinions.
Thank you
Ade
推荐答案
使用数组
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.
这篇关于询问:将多个新行替换为单个新行或br的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文