Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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# 从字符串中删除无法识别的ASCII字符_C#_String_Ascii - Fatal编程技术网

C# 从字符串中删除无法识别的ASCII字符

C# 从字符串中删除无法识别的ASCII字符,c#,string,ascii,C#,String,Ascii,我正在使用html Agility Pack解析html,有时我会得到一些奇怪的字符串,如:“–欧元”。删除它们的最简单方法是什么?顺便说一句,我正在使用C#。您可能需要首先了解为什么要获取这些字符,并且很可能是编码有问题 var stripped = Regex.Replace("străipped of baâ€d charâ€cters", "[^ -~]", ""); Console.WriteLine(stripped); //outputs "stri

我正在使用html Agility Pack解析html,有时我会得到一些奇怪的字符串,如:“–欧元”。删除它们的最简单方法是什么?顺便说一句,我正在使用C#。

您可能需要首先了解为什么要获取这些字符,并且很可能是编码有问题

        var stripped = Regex.Replace("străipped of baâ€d charâ€cters", "[^ -~]", "");
        Console.WriteLine(stripped); //outputs "stripped of bad characters"
但是,如果确实需要从字符串中删除所有非ascii字符,则正则表达式[^-~]会起作用

        var stripped = Regex.Replace("străipped of baâ€d charâ€cters", "[^ -~]", "");
        Console.WriteLine(stripped); //outputs "stripped of bad characters"

有关regex工作原理的解释,请参见。

听起来像是一个编码问题……当它失败时,请检查编码未识别的字体对象-定义似乎确实很棘手。有整个网站都致力于此主题。
        var stripped = Regex.Replace("străipped of baâ€d charâ€cters", "[^ -~]", "");
        Console.WriteLine(stripped); //outputs "stripped of bad characters"