5

Possible Duplicate:
Can I convert a C# string value to an escaped string literal

How can I print a string (via Console.WriteLine()) such that any/all escape sequences that may exist in the string is printed verbatim?

Example:

string s = "This \r\n has \t special \\ characters.";
Console.WriteLine(s);

/* Output (I don't want this)

This 
 has    special \ characters.

*/

I want the output to read:

This \r\n has \t special \\ characters.

Note that in my application I am receiving the string (which contains the escape sequences) from a third party - i.e. If I were creating the string myself, I'm aware that I could just do

string s = @"This \r\n has \t special \\ characters.";
Community
  • 1
  • 1
Jed
  • 10,649
  • 19
  • 81
  • 125
  • So we know what you want to output for characters `xD`, `xA`, `x9` and `x5C`, but what do you want for all the others? If you want the C# compiler's interpretation, chase the ?dupe link above; if not, we'd need a more detailed spec, I think. – AakashM Feb 23 '12 at 18:06
  • @VBRonPaulFan - Thanks for the heads-up on this being a duplicate. The link you gave is what I was looking for. – Jed Feb 23 '12 at 18:13

1 Answers1

5

The only way to do this is to decode the escape sequences yourself and put them back as literal entries into the string. This would require some sort of conversion function. For example

string EscapeIt(string value) {
  var builder = new StringBuilder();
  foreach (var cur in value) {
    switch (cur) {
      case '\t': 
        builder.Append(@"\t");
        break;
      case '\r': 
        builder.Append(@"\r");
        break;
      case '\n':
        builder.Append(@"\n");
        break;
      // etc ...
      default:
        builder.Append(cur);
        break;
    }
  }
  return builder.ToString();
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Could also use Regex, but the basic principle is the same. You also need to escape \ to be \\. – Servy Feb 23 '12 at 18:03
  • @Servy a `Regex` is a pretty heavy solution for something which can be done with a quick switch / case. Note I don't need to esacpe the \ in my solutin because i used verbatim strings – JaredPar Feb 23 '12 at 18:05
  • There's already a comment on the original post showing that this is a duplicate question and an answer that doesn't require you to write out a case statement for every single character. – Kyle W Feb 23 '12 at 18:06
  • 2
    @KyleW that's not a justification for a down vote. My answer was added before the comment. If you feel the question is in error down vote that. Answers shouldn't be downvoted unless they're incorrect. – JaredPar Feb 23 '12 at 18:06
  • If the input strings aren't extremely large then the 'heaviness' isn't a big deal, so it merely comes down to what is quickest/easiest. I would imagine that this is more for debugging purposes than something that is sitting out in production code. As for escaping \, I mean that you need to add a case '\\' append, @"\" clause, not that you didn't properly escape everything you listed in your code. – Servy Feb 23 '12 at 18:08
  • @Servy you don't need to add an explicit case for \. If it's already embedded as a backslash then calling `Append` will readd it as backslash. The escaping of \ with other characters only happens when it's a part of a string constant. – JaredPar Feb 23 '12 at 18:11
  • @JaredPar If the input string is "\\\t" you don't want to output "\\t", which is what would happen if there wasn't a case for '\\' – Servy Feb 23 '12 at 18:14
  • @JaredPar 1) The comment was before your answer, look at the timestamps. 2) While technically it would work, it's a very poor way of doing something that you can do in a couple lines. That's why it gets a downvote. – Kyle W Feb 23 '12 at 18:16
  • @KyleW the comment was posted before my answer was posted. That has little relation to when they were started. It certainly wasn't there when I was typing up my answer. In either case downvotes should be reserved for incorrectness. – JaredPar Feb 23 '12 at 19:33
  • @Servy if the input string was `"\\\t"` then it would come down as 2 characters: \, `\t`. Each would go through the switch in order and correctly output to the buffer – JaredPar Feb 23 '12 at 19:34
  • @JaredPar If there was no case for '\\' then \ and \t would go through the foreach. '\\' would be written as '\\' and would be displayed as '\'. '\t' would be written as '\\t' and be displayed as '\t'. The final result would turn "\\\t" into "\\t". Therefore you are not unescaping the escape character, which is an error. – Servy Feb 23 '12 at 19:40
  • @Servy the OP's question is how to print out a verbatim string. In a verbatim string a backslash is displayed as a single backslash. – JaredPar Feb 23 '12 at 20:20
  • @JaredPar Why don't you take your code, put into it the example that the OP gave, and compare it to both the output he says he doesn't want and the output he says he does want. – Servy Feb 23 '12 at 21:20
  • @JaredPar You may reserve downvotes for incorrect answers. I use them on incorrect or poor answers. – Kyle W Feb 23 '12 at 22:38