1

Is there a built method in .Net for C-style escaping of strings?

For example, I have to convert a string which contains quotes like "hello", and write it as an escaped string \"hello\".

Actually, to be more precise:

string original = "\"hello\"";

should be converted to

string what_i_need = "\\\"hello\\\"";

I could have probably done it myself while writing this question, but I don't want to reinvent hot water.

[Edit] According to the provided answer, this is actually a duplicate of: Can I convert a C# string value to a string literal. It didn't pop out since there were no tags and keywords I was looking for.

Community
  • 1
  • 1
Groo
  • 47,804
  • 16
  • 116
  • 191
  • And how will you provide the string to that function? "\"hello\"" without escape sequence is invalid string. – ata Jan 15 '10 at 13:55
  • Why invalid? It's a string which contains quotes, and I need to escape those quotes (and/or other "special" characters) before writing the string to a text file. Otherwise a third party app is unable to read the file - it considers a quote to be the end of the string and fails. – Groo Jan 15 '10 at 13:59
  • I mean "\"hello"\" without \ is not a string even if you use @ – ata Jan 15 '10 at 14:03

1 Answers1

1

I dont think there is any built in methods. But if you have to write your own, Can I convert a C# string value to a string literal post maybe helpful

Community
  • 1
  • 1
ata
  • 8,625
  • 6
  • 37
  • 66
  • +1, thanks, I will check the code provided by Cristi Diaconescu: http://stackoverflow.com/questions/323640/can-i-convert-a-c-string-value-to-a-string-literal/323670#323670. – Groo Jan 15 '10 at 14:31