将连续空格替换为单个空格和多个“ ”元素

发布于 2024-12-06 03:25:50 字数 268 浏览 1 评论 0原文

我有一个 html 文档,其中某些节点中包含空格。例如,

<B>This is          Whitespace      Node </B>

当这个html在浏览器中显示时,html中多个连续的空格总是显示为一个空格。为了避免这个问题,我想用一个空格和多个 &nbsp; 元素替换连续空格。

实现这一目标的最佳解决方案是什么?

我正在使用 C# 2005。

I have one html document which contains whitespaces in some nodes. For example,

<B>This is          Whitespace      Node </B>

When this html is displayed in the browser, more than one continuous space in html is always displayed as one space. To avoid this issue, I want to replace the continuous spaces with a single space and multiple   elements.

What is the best solution to achive this?

I am using C# 2005.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慵挽 2024-12-13 03:25:50

试试这个,

string str = "<B>This is          Whitespace      Node </B>";
Regex rgx = new Regex("([\\S][ ])");
string result = rgx.Replace(str, "$1.")
                        .Replace(" .","?")
                        .Replace(" "," ")
                        .Replace("?"," ");

Try this,

string str = "<B>This is          Whitespace      Node </B>";
Regex rgx = new Regex("([\\S][ ])");
string result = rgx.Replace(str, "$1.")
                        .Replace(" .","?")
                        .Replace(" "," ")
                        .Replace("?"," ");
忆依然 2024-12-13 03:25:50

按照 使用 CSS 的 white-space 属性http://www.w3.org/TR/CSS2/text.html#white-space-prop

white-space: pre-wrap

或者,如果您确实想用暴力破解,请将两个连续空格替换为不间断空格和一个正常的空间...我强烈建议不要这样做。

string text = originalText.Replace("  ", "  ");

Use CSS's white-space property as per http://www.w3.org/TR/CSS2/text.html#white-space-prop

white-space: pre-wrap

Or, if you really want to do it with bruteforce, replace two consecutive spaces with a non-breaking-space and a normal space... I strongly recommend against this.

string text = originalText.Replace("  ", "  ");
天荒地未老 2024-12-13 03:25:50

您可以尝试

String.Replace("  ", " ")

如果您喜欢正则表达式,

Regex rgx = new Regex("([ \t]|&nsbp)+");
string result = rgx.Replace(input, " ");

You can try

String.Replace("  ", " ")

if you prefer regex

Regex rgx = new Regex("([ \t]|&nsbp)+");
string result = rgx.Replace(input, " ");
最初的梦 2024-12-13 03:25:50

我假设您正在从后面的代码设置控件的值?如果是这样,那么......

<strong><asp:Literal id="myLiteral">This is          Whitespace      Node </asp:Literal></strong>

并且在代码后面......

var myText = "This is          Whitespace      Node ";
myLiteral.Text = myText.Replace(" ", " ");

如果没有代码后面或不在文字中......

<strong><%= "This is          Whitespace      Node ".Replace(" ", " ") %></strong>

I assume you are setting the value of the control from code behind? If so then ...

<strong><asp:Literal id="myLiteral">This is          Whitespace      Node </asp:Literal></strong>

And in code behind ...

var myText = "This is          Whitespace      Node ";
myLiteral.Text = myText.Replace(" ", " ");

If no code behind or not in a literal ...

<strong><%= "This is          Whitespace      Node ".Replace(" ", " ") %></strong>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文