0

please look at these codes :

Health = HttpUtility.HtmlDecode(Health).Replace("%", string.Empty).Replace("\"", string.Empty).Replace("‭‎",string.Empty).Trim();
File.WriteAllText(@"d:\a.txt", Health);
char[] ar = Health.ToCharArray();
File.WriteAllText(@"d:\a.txt", string.Empty);
foreach (char a in ar)
{
    File.AppendAllText(@"d:\a.txt", a.ToString() + Environment.NewLine);
}

int a = int.Parse(Health); //-> I Always Have Error In This Line

the output of d:\a.txt is like :
‎<br> ‭<br> ‭<br> 1
0
0
‬<br> ‬<br> ‎<br> there are 6 hidden and strange characters in that file and the Length of ar array is 9.
what are those hidden characters and how can i remove them?
why Trim() couldn't remove those hidden characters?

4

2 回答 2

3

删除所有不可打印的内容:

var str = "kljdfssdflksdfkl\x03kdkddk\x04lkdldök";
var onlyPrintableChars = str.Where(ch => !char.IsControl(ch)).ToArray();
var resultStr = new string(onlyPrintableChars);
于 2013-07-12T17:56:13.780 回答
2

即使您删除了不可打印的字符,int.Parse如果字符串中有非数字字符,您也可能会引发异常。您可能想使用int.TryParse

int a;
if (!int.TryParse(Health, out a))
{
    // error: non-numeric
}

从事物的外观来看,您正在尝试删除不是数字的所有内容(否则您不会int.Parse对结果进行操作)。如果这就是你想要做的,那么你可以写:

Health = Regex.Replace(Health, "[^0-9]", "");

不过,这可能是个坏主意,因为它会"12foobar34"变成"1234".

您可能应该弄清楚那些坏字符是什么以及它们如何进入您的数据。然后尽快将它们从输入中删除。或者,更好的是,首先阻止他们到达那里。

于 2013-07-12T18:27:12.893 回答