简体   繁体   English

从字符串中剥离字符

[英]Stripping Character From String

I have a code which sends a POST request to a web application and parses the response. 我有一个将POST请求发送到Web应用程序并解析响应的代码。

I'm sending the request with following code: 我正在使用以下代码发送请求:

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

Converting byte data to string with this: 使用以下方法将byte数据转换为string

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

responsefromserver equals something like this: "abcd" responsefromserver等于以下内容: "abcd"

I want to strip " characters. Therefore, I'm using following method: 我要去除"字符。因此,我正在使用以下方法:

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

But '' shows me this error: Empty character literal 但是''向我显示此错误: Empty character literal

When I try to use string.Empty instead of '' , I get this error: Argument 2: cannot convert from 'string' to 'char' 当我尝试使用string.Empty而不是'' ,出现此错误: Argument 2: cannot convert from 'string' to 'char'

What should I do to strip " characters from my string? 我应该怎么做脱光"从我的字符串中的字符?

There is no Char.Empty like there is a String.Empty , so you'll need to use the overload of String.Replace which accepts a String for both arguments. 没有String.Empty这样的Char.Empty ,因此您需要使用String.Replace的重载,这两个参数都接受String

You will need to escape the double quotes, so it should be : 您将需要转义双引号,所以应该是:

Replace("\"", "");

Or: 要么:

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

You have some options: 您有一些选择:

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

In your case you could use Trim: this has the added bonus of removing the character only from the first/last place of the string allowing the rest of it to contain ": 在您的情况下,您可以使用Trim:这具有额外的好处,即仅从字符串的第一个/最后一个位置删除字符,从而使其余字符包含“:”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM