简体   繁体   English

c# 在文本框中用换行符替换“\n”文本

[英]c# replace "\n" text with newline in a textbox

I have some text (for example "o\nour first place, and this\n\n13") and I want that for each "\n" string founded into the text this must be replaced with newline...我有一些文本(例如“o\nour first place, and this\n\n13”),我希望对于文本中的每个“\n”字符串,都必须用换行符替换...

output for the example will be:例如 output 将是:

o our first place, and this o 我们的第一名,而这个

13 How can I make? 13 我该如何制作? textbox is multiline文本框是多行的

the code is代码是

string text_str = txtbox.Text;
text_str .Replace("(?<!\r)\n", "\r\n");
txtbox.Clear();
txtbox.Text = text_str;

I think you're looking for something like this: 我认为您正在寻找这样的东西:

string text_str = txtbox.Text;
text_str = text_str.Replace("\\n", "\r\n");
txtbox.Clear();
txtbox.Text = text_str;

Although that's a really round about way of doing things. 尽管那是一回事。 This will accomplish the same thing: 这将完成同一件事:

txtbox.Text = txtbox.Text.Replace("\\n", "\r\n");

Here you are: 这个给你:

string text_str = "o\nour first place, and this\n\n13";
text_str = text_str.Replace("\n", "\r\n");

Hope this help. 希望对您有所帮助。

这应该工作:

txtbox.Text = txtbox.Text.Replace("\\n", Environment.NewLine);

Note very sure what the OP needed, but in case someone else comes here looking for what I was:注意非常确定 OP 需要什么,但万一有人来这里寻找我的东西:

blah.Text = Regex.Replace(origString, "(?<!\\r)\\n", "\\r\\n")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM