5

I'm trying to escape \ and " in my string like this:

text.Replace("\\", "\\\\").Replace("\"", "\\\"");

But the result for text,

arash "moeen"

turns out as

arash \\\"moeen\\\"

How can I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
arash moeen
  • 4,533
  • 9
  • 40
  • 85
  • What is `text` at start? You are replacing \ with \\ and `"` with `\"`. – Soner Gönül Sep 30 '14 at 07:37
  • Why do you need to escape them? – zerkms Sep 30 '14 at 07:38
  • text = arash "moeen" – arash moeen Sep 30 '14 at 07:38
  • 1
    So you want your resulting text to turn out as: `arash \"moeen\"`? – Marlon Sep 30 '14 at 07:38
  • as they tend to break my json string when I parse it. – arash moeen Sep 30 '14 at 07:39
  • @Marlon yes, exactly that what I want my result to be. – arash moeen Sep 30 '14 at 07:39
  • @arashmoeen have you considered using any well known json serializer? I understand in some cases you may want light weight solution, and hence a simple hackish (not necessarily bad) solution.. but only if you are sure you want your own solution. – Vikas Gupta Sep 30 '14 at 07:45
  • @VikasGupta that's true, I should be considering that as a correct solution anyway. I assume c# comes with a built in json serializer. – arash moeen Sep 30 '14 at 07:48
  • @arashmoeen .Net (not C#) does have couple built in that I know of.. `JavaScriptSerializer` and `DataContractJsonSerializer`. Given the original question has been posed as a string manipulation question, I am not sure how exactly you could utilize these classes.. but there they are for you reference.. Some other external libraries like Json.Net *may* have helper functions, which could potentially help with such escaping rather than full fledged serialization, should you need only escaping. – Vikas Gupta Sep 30 '14 at 07:57
  • The canonical is *[Can I escape a double quote in a verbatim string literal?](https://stackoverflow.com/questions/1928909/)*. This was the third hit in a search engine result and the second in another (probably because the title contains "C#" which the canonical questions don't). – Peter Mortensen Oct 07 '21 at 15:32

6 Answers6

6

Just use @ for verbatim literal strings.

text.Replace(@"this", @"that");

Example:

text.Replace(@"\", @"\\").Replace(@"""", @"\""");
B.K.
  • 9,982
  • 10
  • 73
  • 105
3

What is your assignment's code?

It should be

var text = @"arash ""moeen""";
Nameless One
  • 1,615
  • 2
  • 23
  • 39
3

If I understand correctly, first of all, text = arash "moeen" is not a valid regular string literal. I assume your string like;

string s = "text = arash \"moeen\"";

which is printed as

text = arash "moeen" // I think this is your original string.

Since you arash \"moeen\" as a result, you just need to replace your " with \" in your string like;

string s = "text = arash \"moeen\"";
s = s.Replace("\"", "\\\"");

So your result will be arash \"moeen\"

More information: Escape Sequences

If this is a JSON string, my answer will be invalid :-p

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
2
string text = @"arash ""moeen""";
MessageBox.Show(text.Replace(@"\", @"\\").Replace(@"""", @"\"""));
Alexey
  • 1,521
  • 1
  • 13
  • 24
0

Suppose you have string like this

string test = "He said to me, \"Hello World\". How are you?";

The string has not changed in either case - there is a single escaped " in it. This is just a way to tell C# that the character is part of the string and not a string terminator.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vidy
  • 1,572
  • 6
  • 24
  • 43
-2

When you want arash \"moeen\" from arash "moeen", do text.Replace(", \");.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sybren
  • 1,071
  • 3
  • 17
  • 51