0

I have a following line of code: string str = "\"\\trandom string\\n\"";

and now I need a method to get: string result = "\trandom string\n";

Just like how the compiler deals with quoted strings.
Note that the string str is random, which means any character, including '\t', '\n', '\"', etc may appear in the result.

My question is: Is there an existing method in C# that I can use? If not, do you have any suggestions?

EDIT:
What I want is:
"\\t" -> '\t'
"\\n" -> '\n'
"\\\\" -> '\\'
etc. anything that is not literal within the quotation marks

Michael Kim
  • 689
  • 5
  • 20
  • 3
    What exactly are you trying to achieve? – rae1 Jan 29 '14 at 14:35
  • @rae1 I need scripting for my program – Michael Kim Jan 29 '14 at 14:39
  • So the question is how to convert an instance of a string to its escaped representation? – Steven Liekens Jan 29 '14 at 14:55
  • 1
    I really don't get it. Suppose that your input string is `"a\\tb"`, what do you expect the output string to look like? `a    b`? – Steven Liekens Jan 29 '14 at 15:03
  • @StevenLiekens or is it the opposite? Does he want to unescape `\\t` to `\t` ? @MichaelKim maybe expanding your question to give details of the bigger picture will help us understand what you're trying to achieve. – Greg B Jan 29 '14 at 15:03
  • @GregB that's sort of what I meant. The `\t` is escaped in the input string. The unescaped version is represented by, well, a tab. – Steven Liekens Jan 29 '14 at 15:07
  • @StevenLiekens Sorry I'm not English native, so I have little idea what "escape" here means... but yes, `"a\\tb"` to `a b` is what I want – Michael Kim Jan 29 '14 at 15:08
  • `\t` is what's known as an escape sequence that represents a tab. \\ is also an escape sequence, but this one represents a backslash. `\\t` is really just an escaped backslash followed by a literal `t`. – Steven Liekens Jan 29 '14 at 15:11
  • Argh! These escape sequences are messing with the markdown system. – Steven Liekens Jan 29 '14 at 15:12
  • @StevenLiekens Thank you, unescaping is exactly what I want. And I think I've found the answer here: http://stackoverflow.com/questions/2661169/how-can-i-unescape-and-reescape-strings-in-net – Michael Kim Jan 29 '14 at 15:18

2 Answers2

0

Perfect time for a simple state machine.

edit: added third state for escape char, this state machine assumes you only care about escape characters within the quotes, for one that handles escape characters anywhere it will have more states, but I think this gives a good enough building block.

//define somewhere in your class
enum states { NOT_IN_QUOTES, IN_QUOTES, ESCAPE_CHAR }

//inside a function
states state = states.NOT_IN_QUOTES;
string result = "";
foreach(char c in str)
{
    switch(state)
    {
         case states.NOT_IN_QUOTES:
             if (c == '\"')
             {
                 state = states.IN_QUOTES;
             }
         break;
         case states.IN_QUOTES:
             if (c == '\"')
             {
                 state = states.NOT_IN_QUOTES;
             }
             else if (c == '\\')
             {
                 state = states.ESCAPE_CHAR;
             }
             else
             {
                  result += c;
             }
         break;
         case states.ESCAPE_CHAR:
             state = states.IN_QUOTES;
             result += c;
         break;
     }
}
return result;
Lambage
  • 367
  • 3
  • 8
  • This can only deal with quotation marks. What I need is dealing with things like "\\n", things that are not literal within the quotation marks – Michael Kim Jan 29 '14 at 14:56
0
System.Text.RegularExpressions.Regex.Unescape
Michael Kim
  • 689
  • 5
  • 20