Robert Zunr
Robert Zunr

Reputation: 77

Stripping Character From String

I have a code which sends a POST request to a web application and parses the response.

I'm sending the request with following code:

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

Converting byte data to string with this:

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

responsefromserver equals something like this: "abcd"

I want to strip " characters. Therefore, I'm using following method:

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

But '' shows me this error: Empty character literal

When I try to use string.Empty instead of '' , I get this error: Argument 2: cannot convert from 'string' to 'char'

What should I do to strip " characters from my string?

Upvotes: 1

Views: 707

Answers (2)

Nydirac
Nydirac

Reputation: 21

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 ":

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

Upvotes: 0

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

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.

You will need to escape the double quotes, so it should be :

Replace("\"", "");

Or:

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

Upvotes: 4

Related Questions