html解码的html编码空间不是空间



直到现在我还在想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

HTML 实体 不表示空格

,它表示不间断的空格。

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

string nbspace = "u00A0";

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

string text = " ";
text = HttpUtility.HtmlDecode(HttpUtility.HtmlDecode(text));

我正在像这样清理 html:

  var text = WebUtility.HtmlDecode(html)
      .Replace("u00A0", " ") // Replace non breaking space with space.
      .Replace("  ", " ") // Shrink multiple spaces into one space.
      .Trim();

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

如果您有:

string text = " ";

然后,这将解码为不间断的空格。

您好,几分钟前我遇到了同样的问题。
我是这样解决的:

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

所以现在: text = ""为真(末尾的修剪消除了空间)

最新更新