簡體   English   中英

從字符串中剝離字符

[英]Stripping Character From String

我有一個將POST請求發送到Web應用程序並解析響應的代碼。

我正在使用以下代碼發送請求:

byte [] responseBytes = webClient.UploadValues(“ https://example.com ”,“ POST”,formData);

使用以下方法將byte數據轉換為string

string responsefromserver = Encoding.UTF8.GetString(responseBytes);

responsefromserver等於以下內容: "abcd"

我要去除"字符。因此,我正在使用以下方法:

Console.WriteLine(responsefromserver.Replace('"', ''));

但是''向我顯示此錯誤: Empty character literal

當我嘗試使用string.Empty而不是'' ,出現此錯誤: Argument 2: cannot convert from 'string' to 'char'

我應該怎么做脫光"從我的字符串中的字符?

沒有String.Empty這樣的Char.Empty ,因此您需要使用String.Replace的重載,這兩個參數都接受String

您將需要轉義雙引號,所以應該是:

Replace("\"", "");

要么:

Replace("\"", String.Empty);

您有一些選擇:

responsefromserver.Replace("\"", "");//escaping the "

responsefromserver.Replace('"', '\0'); //null symbol <- not reccomended, only to allow you to use the char overload. It will NOT remove the " but replace with a null symbol!
responsefromserver.Replace('"', (char)0); // same as above in another format

在您的情況下,您可以使用Trim:這具有額外的好處,即僅從字符串的第一個/最后一個位置刪除字符,從而使其余字符包含“:”。

responsefromserver.Trim('"'); // Remove first/last "

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM