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

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

我有一些文本(例如 "o\nour first place, and this\n\n13"),我希望文本中的每个“\n”字符串都必须用换行符替换...

示例的输出将是:

o 我们的第一名,还有这个

13 我该如何制作?文本框是多行的

密码是

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

你在这里:

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

希望对您有所帮助。

我想你正在寻找这样的东西:

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

尽管这确实是一种迂回的做事方式。这将完成同样的事情:

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

这应该有效:

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

注意非常确定 OP 需要什么,但以防其他人来这里寻找我的东西:

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