vb.net html编码空间的HtmlDecode不是空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13565097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:18:40  来源:igfitidea点击:

HtmlDecode of html encoded space is not space

c#asp.netvb.net

提问by user1481853

Till now I was thinking HttpUtility.HtmlDecode(" ")was a space. But the below code always returns false.

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

string text = " ";

text = HttpUtility.HtmlDecode(text);

string space = " ";

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

Same when I try with Server.HtmlDecode()

当我尝试使用时相同 Server.HtmlDecode()

Why is it so?

为什么会这样?

Any help would be much appreciated

任何帮助将非常感激

Thanks, N

谢谢,N

回答by Guffa

The HTML entity  doesn't represent a space, it represents a non-breaking space.

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

The non-breaking space has character code 160:

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

string nbspace = "\u00A0";


Also, as Marc Gravell noticed, you have double encoded the code, so you would need to decode it twice to get the character:

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

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

回答by mkaj

I'm cleaning the html like this:

我正在像这样清理 html:

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

回答by Marc Gravell

The HTML of  doesn't mean anykind of space. It means, literally, the text  - for example, if you were writing HTML that was talking about HTML, you may need to include the text  , which you would do by writing the HTML  .

的 HTML 并不意味着任何类型的空间。它的字面意思是文本 - 例如,如果您正在编写谈论 HTML 的 HTML,您可能需要包含 text  ,您可以通过编写 HTML 来实现 

If you had:

如果你有:

string text = " ";

then thatwould decode to a non-breakingspace.

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

回答by Astrea

Hello I faced the same issue some minutes ago.
I solved it in this way:

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

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

so now: text = ""is true (the Trim at the end eliminates the space)

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