Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C语言解码HTML文本#_C#_Html_.net_Unit Testing_Nunit - Fatal编程技术网

C# 用C语言解码HTML文本#

C# 用C语言解码HTML文本#,c#,html,.net,unit-testing,nunit,C#,Html,.net,Unit Testing,Nunit,我有一个助手方法: public static string StripHtml(this string text) { text = HttpUtility.HtmlDecode(text); text = Regex.Replace(text, @"<(.|\n)*?>", ""); return text; } 当我运行单元测试时,它失败并出现以下错误: String lengths are both 3. Strings differ at in

我有一个助手方法:

public static string StripHtml(this string text)
{
    text = HttpUtility.HtmlDecode(text);
    text = Regex.Replace(text, @"<(.|\n)*?>", "");

    return text;
}
当我运行单元测试时,它失败并出现以下错误:

 String lengths are both 3. Strings differ at index 1.
  Expected: "A B"
  But was:  "A B"
  ------------^

所以我的问题是,为什么
没有被解码成空格字符?

被解码成一个不间断的空格字符,而不是空格字符,查找相同的用户,但却是另一个字符

如果按alt+255,这是不间断空格alt+32,如果要查看,这是诺曼空格


您可以保存文本输出并在十六进制编辑器中查看它,在那里您应该注意到,它得到的十六进制值与普通空格不同。单元测试是正确的。
请参见

HTML实体并不表示空格,它表示不间断的空格。 非中断空格的字符代码为160: 字符串nbspace=“\u00A0”

非中断空间根本不是一个空间,因此如果你想要一个空间,你必须替换它

var text = HttpUtility.HtmlDecode(HttpUtility.HtmlDecode("A&nbsp;B"));
text = Regex.Replace(text, @"<(.|\n)*?>", "");
byte[] c = Encoding.ASCII.GetBytes(text.ToString());
byte[] x = Encoding.ASCII.GetBytes("A B");
var text=HttpUtility.HtmlDecode(HttpUtility.HtmlDecode(“ab”);
text=Regex.Replace(text,@“”);
byte[]c=Encoding.ASCII.GetBytes(text.ToString());
字节[]x=Encoding.ASCII.GetBytes(“AB”);

不间断空格与空格不同。:)这个方法的名字很好。让它也跳舞:)@lazyberezovsky,还没有:)谢谢!我添加了
text=Regex.Replace(text,@“\u00A0”,”)
inside
StripHtml
方法,它工作正常!
var text = HttpUtility.HtmlDecode(HttpUtility.HtmlDecode("A&nbsp;B"));
text = Regex.Replace(text, @"<(.|\n)*?>", "");
byte[] c = Encoding.ASCII.GetBytes(text.ToString());
byte[] x = Encoding.ASCII.GetBytes("A B");