如何从解析的字符串中删除特殊字符
How to remove a special characters from the parsed string
我正在解析网站上某些商品的价格。但是,我在字符串之前得到了一些不相关的特殊字符。如何删除那些字符和我想要的字符串?
我得到
\n \n \n \n \n\n \n \n \n AMD YD2600BBAFBOX 3.9GHz Socket AM4 Processor
和 17,975.00
但是,我已经使用 Replace 方法替换了字符串中不需要的特殊字符
itemName = itemNameNode.InnerText.Replace("\n", "");
itemPrice = itemPriceNode.InnerText.Replace(" ", "Current price:");
我仍然没有得到预期的结果。我得到的结果是
I have linked my image here for reference. It doesn't allow me to post image here (Seriously! Whosebug)
您可以简单地使用 String.Trim,而不是在 itemName
字符串的换行符上进行替换。 Trim 删除字符串中 return 对 char.IsWhiteSpace
调用正确的任何前导或尾随字符,其中包括换行符。
var x = "\n Hello \n";
Console.WriteLine("-");
Console.WriteLine(x);
Console.WriteLine("-");
/* Output:
-
Hello
-
*/
Console.WriteLine("-");
Console.WriteLine(x.Trim());
Console.WriteLine("-");
/* Output:
-
Hello
-
*/
首先,我会这样尝试。
itemName = itemNameNode.InnerText.Trim();
itemPrice = itemPriceNode.InnerText.Trim().Replace(" ", "Current price:");
先用Trim()
再用Replace()
怎么样
希望对你有所帮助
我正在解析网站上某些商品的价格。但是,我在字符串之前得到了一些不相关的特殊字符。如何删除那些字符和我想要的字符串?
我得到
\n \n \n \n \n\n \n \n \n AMD YD2600BBAFBOX 3.9GHz Socket AM4 Processor
和 17,975.00
但是,我已经使用 Replace 方法替换了字符串中不需要的特殊字符
itemName = itemNameNode.InnerText.Replace("\n", "");
itemPrice = itemPriceNode.InnerText.Replace(" ", "Current price:");
我仍然没有得到预期的结果。我得到的结果是
I have linked my image here for reference. It doesn't allow me to post image here (Seriously! Whosebug)
您可以简单地使用 String.Trim,而不是在 itemName
字符串的换行符上进行替换。 Trim 删除字符串中 return 对 char.IsWhiteSpace
调用正确的任何前导或尾随字符,其中包括换行符。
var x = "\n Hello \n";
Console.WriteLine("-");
Console.WriteLine(x);
Console.WriteLine("-");
/* Output:
-
Hello
-
*/
Console.WriteLine("-");
Console.WriteLine(x.Trim());
Console.WriteLine("-");
/* Output:
-
Hello
-
*/
首先,我会这样尝试。
itemName = itemNameNode.InnerText.Trim();
itemPrice = itemPriceNode.InnerText.Trim().Replace(" ", "Current price:");
先用Trim()
再用Replace()
希望对你有所帮助