[英]How to remove special characters from start of string but leave in the body of the string?
[英]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
17,975.00
但是,我使用了Replace方法來替換字符串中不需要的特殊字符
itemName = itemNameNode.InnerText.Replace("\n", "");
itemPrice = itemPriceNode.InnerText.Replace(" ", "Current price:");
我仍然沒有得到預期的結果。 我得到的結果
不必在itemName
字符串的換行符上進行替換,只需使用String.Trim即可 。 Trim刪除字符串中所有返回true的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:");
在使用Replace()
之前如何使用Trim()
Replace()
希望對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.