从字符串中剥离字符

时间:2018-08-27 15:40:00

标签: c#

我有一个将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'

我应该怎么做从字符串中去除"个字符?

2 个答案:

答案 0 :(得分:4)

没有{{3}},因此您需要使用String.Replace的重载,这两个参数都接受String

您需要转义双引号,所以应该是:

Replace("\"", "");

或者:

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

答案 1 :(得分:0)

您有一些选择:

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 "
相关问题