-2
  • List item

I have a string object such as the following.

string mycontent = "\"The time is Valid\"\n"
string tocompare = "The time is Valid"

The contents of mycontent are assigned by the following line of code.

string mycontent = await response.Content.ReadAsStringAsync();

I want to be able to remove all the \n and the \ and to do that I do the following.

mycontent.Trim().Replace(@"\","")

I am trying to compare the mycontent and tocompare for equality. But this does not work.

if (string.Compare(tocompare, mycontent.Trim().Replace(@"\","")) == 0)
{
     //TODO
}

For some reason which I do not know, I am not able to see equal to 0.

Am I missing something here? Any help is appreciated.

Anna
  • 181
  • 1
  • 12
  • Why aren't you using ````mycontent = mycontent.Trim().Replace(@"\","")```` – JohnPete22 Oct 09 '19 at 14:27
  • 2
    On top of that, your string [does not](https://stackoverflow.com/q/5465923/11683) actually contain any slashes. – GSerg Oct 09 '19 at 14:28
  • @GSerg I am not sure what you mean by my string does not have any slashes? – Anna Oct 09 '19 at 14:29
  • 2
    @Anna I meant that `mycontent` contains `"The time is Valid"`. It does not contain a slash. – GSerg Oct 09 '19 at 14:30
  • I am trying to compare this received string with another string which holds "The time is Valid" using the Compare method. – Anna Oct 09 '19 at 14:30
  • Please add your comparison so we can try to explain. – mortb Oct 09 '19 at 14:31
  • @mortb Just updated the question – Anna Oct 09 '19 at 14:33
  • `mycontent` has the text in double quotes and with a new line after it. `tocompare` has the text without quotes and without the new line. Neither string contains slashes. And, now that we see more code, the [originally suggested duplicate](https://stackoverflow.com/questions/13277667/c-sharp-string-replace-does-not-actually-replace-the-value-in-the-string) no longer applies. Now it should be Possible duplicate of [Why does .NET add an additional slash to the already existent slashes in a path?](https://stackoverflow.com/q/5465923/11683) – GSerg Oct 09 '19 at 14:36

3 Answers3

1

There are no actual slash characters in the string you showed. The slashes in the source code are part of the string literal syntax, where the \" represents just the character " (it needs to be escaped to distinguish it from the end of the string) and the \n represents a newline.

If you want to remove quotes and newlines from the sides of the string, you can do that with Trim too:

mycontent.Trim('"', '\n')

But that’s a bit of a weird thing to do. If this string actually represents some serialized format for the text you want, like JSON or CSV, you should use a parser for that instead. Where did you get it?

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • This string is obtained from ```string mycontent = await response.Content.ReadAsStringAsync();``` – Anna Oct 09 '19 at 14:34
  • @Anna: And what’s that response? JSON? (If it’s HTTP, for example, look at its `Content-Type` header as a good signal.) – Ry- Oct 09 '19 at 14:35
  • yes, that is a json response – Anna Oct 09 '19 at 14:36
  • @Anna: Okay, you should use a JSON parser then. – Ry- Oct 09 '19 at 14:36
  • I am not sure on how to do that. Can you help me? – Anna Oct 09 '19 at 14:37
  • @Anna: I can’t, beyond linking to questions like https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c that might be out of date, but if you edit your question to mention that you have a JSON string, you’ll probably get better answers. – Ry- Oct 09 '19 at 14:39
0

If you want to add a text slash to a string, you must type like this.

string mycontent = "\\"+"The time is Valid"+"\\"+"\n";

To delete slashes

mycontent=mycontent.Trim().Replace("\\","");
0

You have to replace slash first and then the quote

mycontent = mycontent.Trim().Replace(@"\", "").Replace(@"""", "")

In your case:

if (string.Compare(tocompare, mycontent.Trim().Replace(@"\","").Replace(@"""", "")) == 0)
{
     //TODO
}
David Cárcamo
  • 114
  • 1
  • 4
  • 1
    As there are no slashes in the string, the only replace that does something is the one that replaces the double quotes. – GSerg Oct 09 '19 at 19:29