使用c#将多个enter(\ n\ n)替换为单个输入在richtextbox中的\ n [英] replace multiple enter (\n\n) into single enter \n in richtextbox using c#

查看:130
本文介绍了使用c#将多个enter(\ n\ n)替换为单个输入在richtextbox中的\ n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个richtextbox,其中用户输入一个段落但是当用户输入多个enter(\ n \ nn \ n)然后我想把它换成单个\ n但我不知道这怎么可能。



我用lambda表达式将多个空格替换成单个空格



var list = rtxtTypeing.Text.Split('')。Where(s =>!string.IsNullOrWhiteSpace(s));

rtxtTypeing。 Text = string.Join(,list);



和我想要用来将多个\ n \ n \\ n更换为单个输入\ n



var list3 = rtxtTypeing.Text.Split('\ n')。Where(s =>!string.IsNullOrWhiteSpace(s)) ;

rtxtTypeing.Text = string.Join(\ n,list3);



但它不起作用。



如果我错了,请帮帮我。



谢谢

Ram Kumar

Hi everybody,

I have an richtextbox in which user enter a passage but when user enter multiple enter (\n\n\n) then I want to replace it into single \n but i do not know how this possible.

I have replace multiple space into single space using lambda expression

var list = rtxtTypeing.Text.Split(' ').Where(s => !string.IsNullOrWhiteSpace(s));
rtxtTypeing.Text = string.Join(" ", list);

and same thing i want to use in to replace multiple \n\n\n into single enter \n

var list3 = rtxtTypeing.Text.Split('\n').Where(s => !string.IsNullOrWhiteSpace(s));
rtxtTypeing.Text = string.Join("\n", list3);

but it is not working.

Please help me where I am wrong.

Thanks
Ram Kumar

推荐答案

您可以使用 Regex [ ^ ] 。

假设按钮点击事件有2个富文本框:richTextBox1和richTextBox2以及一个按钮

,添加:

You may use Regex[^].
Say there are 2 richtextboxes: richTextBox1 and richTextBox2 and a button
on the button click event, add this:
string txt1 = richTextBox1.Text;
var pattern = "(\\n){2,}";
var replacePattern = "\n";
richTextBox2.Text =  Regex.Replace(txt1, pattern, replacePattern, RegexOptions.IgnoreCase);


在Windows上,新行是 \\\\ n - CRLF 但在基于Unix的系统上,换行符为 \ n - LF 。这可能是您需要更换的东西之一。



Windows保持程序运行的平台的换行格式为 System。 Environment.NewLine



如果您不介意使用正则表达式。您可以在这两个平台上的一次传递中删除多行,例如:


On Windows, a new line is \r\n - CRLF but on Unix based systems, a newline is \n - LF. That could be one of the things you needed to replace.

Windows keeps the newline format of the platform your program is running in at System.Environment.NewLine

And if you don't mind using Regex. You could remove multiple lines in a single pass on both platforms like this

string formatted = Regex.Replace(rtxtTypeing.Text, @"(?:\r\n|\r(?!\n)|(?!<\r)\n){2,}", System.Environment.NewLine);









用一个br替换多个换行符 [ ^ ]



替换字符串中的换行符 [ ^ ]


这篇关于使用c#将多个enter(\ n\ n)替换为单个输入在richtextbox中的\ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆