I have some user input that I am outputting to a label.
我有一些用户输入,我输出到一个标签。
I have used HTML.Encode in order to show the input in the way the user entered it (ignoring as a html tag).
我已经使用HTML。对输入进行编码,以显示用户输入的方式(忽略为html标记)。
However, I have noticed that the user input like New Line are not using in the label. It's simply displayed as white space.
但是,我注意到用户输入如New Line在标签中没有使用。它只是显示为空白。
I've done this
我做了这个
msg.Text = msg.Text.Replace(Environment.NewLine, "<br />");
which seems to now be displaying the right input.
它现在似乎显示了正确的输入。
Is that the best way to do this? Or is there like a common method that can convert newLines, tabs, etc. all of the invisible formatting things into HTML tags?
这是最好的方法吗?或者有没有一种常见的方法可以将换行符、制表符等所有不可见的格式转换成HTML标签?
2 个解决方案
#1
3
I don't know of any other way. What I usually do (in case you have a single "\n" or a "\r\n" combo) is replace all "\r\n" first, then any single "\n" last.
我不知道还有别的办法。我通常做的(如果你有一个“\n”或“\r\n”组合)是先替换所有“\r\n”,然后再替换任何一个“\n”。
lbl.Text = lbl.Text.Replace("\r\n", "<br />").Replace("\n", "<br />");
For tabs you can use 4 non-breaking spaces.:
对于制表符,你可以使用4个不间断空格。
lbl.Text = lbl.Text.Replace("\t", " ")
To preserve any spacing (as html will aggregate multiple continuous spaces into a single space) use:
为了保持任何间隔(html将多个连续空间聚合为一个空间),使用:
lbl.Text = lbl.Text.Replace(" ", " ")//Replace every 2-space pair.
Remember to Encode your text first before adding in markup like <br /> that you intentionally want to render.
在添加
等有意呈现的标记之前,请记住先对文本进行编码。
You could also use a TextBox, set it's MultiLine property to "true" and Enabled to "false" if you want to display the information with the original carriage returns without resorting to inserting markup. I think the label is the best choice though.
您还可以使用一个文本框,将它的MultiLine属性设置为“true”,如果您希望使用原始的回车来显示信息,而不需要使用插入标记,则可以将其设置为“false”。我认为标签是最好的选择。
#2
0
If possible, just use a Multiline Textbox instead of label.
如果可能的话,使用多行文本框而不是标签。
#1
3
I don't know of any other way. What I usually do (in case you have a single "\n" or a "\r\n" combo) is replace all "\r\n" first, then any single "\n" last.
我不知道还有别的办法。我通常做的(如果你有一个“\n”或“\r\n”组合)是先替换所有“\r\n”,然后再替换任何一个“\n”。
lbl.Text = lbl.Text.Replace("\r\n", "<br />").Replace("\n", "<br />");
For tabs you can use 4 non-breaking spaces.:
对于制表符,你可以使用4个不间断空格。
lbl.Text = lbl.Text.Replace("\t", " ")
To preserve any spacing (as html will aggregate multiple continuous spaces into a single space) use:
为了保持任何间隔(html将多个连续空间聚合为一个空间),使用:
lbl.Text = lbl.Text.Replace(" ", " ")//Replace every 2-space pair.
Remember to Encode your text first before adding in markup like <br /> that you intentionally want to render.
在添加
等有意呈现的标记之前,请记住先对文本进行编码。
You could also use a TextBox, set it's MultiLine property to "true" and Enabled to "false" if you want to display the information with the original carriage returns without resorting to inserting markup. I think the label is the best choice though.
您还可以使用一个文本框,将它的MultiLine属性设置为“true”,如果您希望使用原始的回车来显示信息,而不需要使用插入标记,则可以将其设置为“false”。我认为标签是最好的选择。
#2
0
If possible, just use a Multiline Textbox instead of label.
如果可能的话,使用多行文本框而不是标签。