13

直到现在我还在想HttpUtility.HtmlDecode(" ")是一个空间。但是下面的代码总是返回 false。

string text = " ";

text = HttpUtility.HtmlDecode(text);

string space = " ";

if (String.Compare(space, text) == 0)
  return true;
else
  return false;

当我尝试时也一样Server.HtmlDecode()

为什么会这样?

任何帮助将非常感激

谢谢, N

4

4 回答 4

16

HTML 实体 不代表一个空格,它代表一个不间断的空格。

不间断空格的字符代码为 160:

string nbspace = "\u00A0";

此外,正如 Marc Gravell 所注意到的,您已经对代码进行了双重编码,因此您需要对其进行两次解码才能获得字符:

string text = " ";
text = HttpUtility.HtmlDecode(HttpUtility.HtmlDecode(text));
于 2012-11-26T12:46:15.587 回答
3

我正在像这样清理html:

  var text = WebUtility.HtmlDecode(html)
      .Replace("\u00A0", " ") // Replace non breaking space with space.
      .Replace("  ", " ") // Shrink multiple spaces into one space.
      .Trim();
于 2016-10-14T23:12:34.417 回答
2

的 HTML 并不意味着任何类型的空间。从字面上看,它意味着文本 ——例如,如果您正在编写 HTML 并谈论 HTML,您可能需要包含 text  ,您可以通过编写 HTML 来做到这一点 

如果你有:

string text = " ";

那么将解码为一个不间断的空间。

于 2012-11-26T12:47:07.433 回答
1

您好,几分钟前我遇到了同样的问题。
我以这种方式解决了它:

string text = " ";
text = Server.HtmlDecode(text).Trim;

所以现在: text = ""是真的(最后的修剪消除了空间)

于 2020-05-05T09:19:35.000 回答