0

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?

Robert Zunr
  • 67
  • 1
  • 11

2 Answers2

4

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);
David Zemens
  • 53,033
  • 11
  • 81
  • 130
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • One of the overloads for `Replace` accepts a `Char`, but to use replace with a string replacement, you need to use the 2nd overload, which expects a string as the first argument. – David Zemens Aug 27 '18 at 15:46
  • 1
    Edited your answer to improve it, did not seem worthwhile to create a new answer that arrives at the same solution. Cheers. – David Zemens Aug 27 '18 at 15:49
  • @CamiloTerevinto thanks for it, never noticed that overload – Ehsan Sajjad Aug 27 '18 at 15:51
0

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 "
Nydirac
  • 21
  • 1
  • 3