C# 从字符串中剥离字符
我有一个向web应用程序发送POST请求并解析响应的代码 我正在发送带有以下代码的请求: byte[]responseBytes=webClient.UploadValues(“,”POST“,formData) 使用以下命令将C# 从字符串中剥离字符,c#,C#,我有一个向web应用程序发送POST请求并解析响应的代码 我正在发送带有以下代码的请求: byte[]responseBytes=webClient.UploadValues(“,”POST“,formData) 使用以下命令将字节数据转换为字符串: string responsefromserver = Encoding.UTF8.GetString(responseBytes); responsefromserver等于这样的内容:“abcd” 我想去掉“字符。因此,我使用以下方法: Co
字节
数据转换为字符串
:
string responsefromserver = Encoding.UTF8.GetString(responseBytes);
responsefromserver
等于这样的内容:“abcd”
我想去掉“
字符。因此,我使用以下方法:
Console.WriteLine(responsefromserver.Replace('"', ''));
但是'
向我显示了这个错误:空字符文本
当我尝试使用string.Empty
而不是'
时,我遇到了以下错误:参数2:无法从“string”转换为“char”
如何从字符串中删除“
字符?没有,因此您需要使用字符串的重载。替换为接受两个参数的字符串
您需要对双引号进行转义,因此应该是:
Replace("\"", "");
或:
没有,因此您需要使用String.Replace的重载,该重载接受两个参数的String
您需要对双引号进行转义,因此应该是:
Replace("\"", "");
或:
您有一些选择:
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
您有一些选择:
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
使用responsefromserver.Replace(“\”,string.Empty)
。使用responsefromserver.Replace(“\”,string.Empty)
。Replace
的一个重载接受Char
,但要使用Replace替换字符串,需要使用第二个重载,它需要一个字符串作为第一个参数。编辑您的答案以改进它,似乎不值得创建得到相同解决方案的新答案。干杯。@CamiloTerevinto谢谢,从来没有注意到重载Replace
的一个重载接受一个Char
,但是要用字符串替换来使用Replace,您需要使用第二个重载,它需要一个字符串作为第一个参数。编辑您的答案以改进它,似乎不值得创造一个新的答案,得出相同的解决方案。干杯。@Camiloterivento谢谢你,从来没有注意到超载