1

The goal is to have a string input (coming from the frontend), and this string should be transformed to act as a escaped char in the backend.

In the following example I want the user to write "\" + "t", and the backend should interpret it as "\t" (= tab char):

    var inputStr = @"\t"; // The input is a string written by a user: "\t" (backslash char + t char == @"\t" != "\t")
    var outputStr = SomeOperation(inputStr); // ???
    Console.WriteLine("A" + outputStr + "B <= should be tab separated");

I have tried:

var outputStr = inputStr.Replace("\", "");
Xavier Peña
  • 7,399
  • 9
  • 57
  • 99
  • 3
    Are you asking how to parse the string `\t` into a tab character? – haim770 Nov 20 '18 at 11:33
  • 1
    Maybe this is what you're looking for: `var outputStr = inputStr.Replace(@"\", "\\");`? – Oram Nov 20 '18 at 11:34
  • Sorry for the confusion, I have edited the post to try to clarify what I am trying to achieve: `The goal is to have a string input (coming from the frontend) that should act as a escaped char in the backend. In the following example I want the user to write "\" + "t", and the backend should interpret it as "\t" (= tab char).` – Xavier Peña Nov 20 '18 at 11:35
  • Not possible out of the box afaik. You would have to build our own function. This might help to get you started: https://stackoverflow.com/questions/323640/can-i-convert-a-c-sharp-string-value-to-an-escaped-string-literal – marsze Nov 20 '18 at 11:35
  • Possible duplicate of [Can I expand a string that contains C# literal expressions at runtime](https://stackoverflow.com/questions/3298075/can-i-expand-a-string-that-contains-c-sharp-literal-expressions-at-runtime) – marsze Nov 20 '18 at 11:37
  • @Oram It doesn't seem to work. – Xavier Peña Nov 20 '18 at 11:39
  • as @marc-gravell pointed out, only inputStr.Replace(@"\t","\t") will work – Surenthar Pitchai Nov 20 '18 at 11:47

4 Answers4

1

This isn't something that is built in. Ultimately, "\t" == (a string of length 1 containing a tab character) is implemented by the C# compiler, not the runtime. There isn't a pre-existing implementation of this in the runtime, in part because each language (VB.NET, C#, F#, etc) can have their own rules.

You would need to write your own implementation with your own definitions of escape characters. Fortunately, it is mostly an exercise in .Replace(...). There are some edge cases to think about - in particular for ordering - though; for example, if \\ becomes \ and \n becomes newline; does \\n become \n? or does it become \(newline)? done naively, it can end up as just (newline) - i.e. foo.Replace(@"\\",@"\").Replace(@"\n","\n")

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I see. I was wondering: if we respect the hypothesis "just one single output char is expected", could there be way to convert the input string into a escaped char? Example: "," becomes "," (no change), "\\n" becomes "\n", and maybe even "\\u1F642" (= thumbs up unicode char) becomes "\u1F642". The range of assumptions that are needed seems much more limited in this specific case. – Xavier Peña Nov 20 '18 at 11:50
  • @XavierPeña ultimately, it is your system - so : if that hypothesis/assumption fits your needs: great, do that – Marc Gravell Nov 20 '18 at 11:51
  • I was asking for help about _if_ this could be done (with the restrictive hypothesis) and _how_ it could be done. One of the comments is showing a hint: https://stackoverflow.com/questions/323640/can-i-convert-a-c-sharp-string-value-to-an-escaped-string-literal, so I am guessing it is possible. – Xavier Peña Nov 20 '18 at 12:00
0

You can do something like this:

void Main()
{
    Debug.Assert(ReplaceChar("hello\tworld", @"\t") == "helloworld"); // passed
}

string ReplaceChar(string str, string userInput)
{
    switch (userInput)
    {
        case @"\t":
            return str.Replace("\t","");
    }
    return str;
}
Mohi
  • 1,776
  • 1
  • 26
  • 39
0

I have finally found an easy way to do it:

Regex.Unescape(inputStr);

See the documentation of the Regex.Unescape function for more details.

Example:

var ouptutStr = Regex.Unescape("\\t"); 
// ✓ Result: outputStr == "\t"
Xavier Peña
  • 7,399
  • 9
  • 57
  • 99
-1

var outputStr = Char.Parse("\t").ToString(); gives

A B <= should be tab separated

It isn't seen here but in console it looks properly.

Miamy
  • 2,162
  • 3
  • 15
  • 32
  • It shows an exception: `String must be exactly one character long.`. In your code, you are using "\t" instead of "\\t" (or @"\t"), which is the tab character in itself. – Xavier Peña Nov 20 '18 at 11:43
  • Yes, but `"\t"` argument gives me the real tab symbol in console. – Miamy Nov 20 '18 at 11:47