繁体   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