且构网

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

如何识别Web文本区域中的新行

更新时间:2023-12-05 10:57:46

TextArea使用carrage-return('\ r'),换行('\ n')样式换行符号。您甚至可能在调试器中看不到字符,因为它们将被解释为这些显示中的新行。



它们可能会通过换行输出到您的html页面:

TextArea uses carrage-return('\r'), line-feed('\n') style chars for line breaks. You may not see the chars even in debugger as they will be interpreted to new lines in those displays.

They may well be output to your html page with the line break:
<div>
HI,
i am good. 
</div>



但浏览器会在没有< br />的情况下忽略它,正如您所说的那样



试试这个:


but the browser will ignore it without the <br />, as you correctly stated

Try this:

//C#
var stringAsHtml = myTextArea.Text.Replace("\r\n", "<br />\r\n");




//Javascript
var stringAsHtml =


(选择器)。 val()。替换( \\\\ n &l t; br /> \\\\ n);
(selector).val().Replace("\r\n", "<br />\r\n");





如果将保留换行符标记,使其更容易阅读。然后输出如下:





If will preserve the line break in the markup to make it easier to read. the output with then be like this:

<div>
HI, <br />
i am good.
</div>







希望有所帮助^ _ ^




Hope that helps ^_^