2

I tried to use string interpolation $"" in C#6 with tabs

 var name="My Name";
 var text =$"\t\t{name}";

and it's working fine and tabs \t is resolved.

When trying to use Long string interpolation lines

      var name = "myname";
      var text = $@"\t\t{name}
                    tab and name  is in a Long string interpolation \r\n
       ";
    Console.WriteLine(text);

output

     \t\tmyname
                    tab and name  is in a Long string interpolation \r\n  

The tab \t ,\r and \n aren't resolved

So i had to use string.Format() to resolve this issue.

The question:

Is this a limitation in the Long string interpolation for not supporting \t \r \n in c#6 (even c#7)

M.Hassan
  • 10,282
  • 5
  • 65
  • 84

2 Answers2

4

You have the verbatim modifier @ in front of that string, so your tab characters will be un-escaped and treated as normal text. If you want to include them in the string, then you can either enclose the characters in curley brackets (since you're also using the $ string interpolation modifier) so they're treated as tabs (same with the carriage return and newline characters):

    var name = "myname";
    var text = $@"{"\t\t"}{name}
            tab and name  is in a Long string interpolation {"\r\n"}
";
    Console.WriteLine(text);

Alternatively, since it's a verbatim string, you can just press Tab (or Enter) keys where you want those characters in the string.

This string is the same as the one above:

    var text = $@"      {name}
            tab and name  is in a Long string interpolation 

";
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • 1
    "Alternatively, since it's a verbatim string, you can just press Tab" Except if you have plugin or setting that changes tabs to spaces. – Kenneth K. Aug 23 '18 at 18:18
  • Enclosing the characters in curley brackets resolved \t \r \n, – M.Hassan Aug 23 '18 at 18:19
  • @KennethK. - yes, but if you're living in a state of sin already (following the "spaces are superior to tabs" heathens), you deserve everything you get :-) – Damien_The_Unbeliever Aug 23 '18 at 18:32
  • @RufusL you are exactly correct and it seems that whilst i was running through similar topics for VB.NET, I glossed over your second example. Removing. – Enzoaeneas Feb 24 '20 at 16:20
3

You are using verbatim string in your second example so \t is being escaped. It has nothing to do with string interpolation.

If you want to use tab character don't use verbatim string. You can concatenate multiple lines using string concatenation with "+"

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • Or, you know, insert a literal tab rather than `\t`. The only thing (Saying this, I'm bound to have forgotten something) you cannot write *literally* in a verbatim string without special work is the double quote. – Damien_The_Unbeliever Aug 23 '18 at 18:06
  • 1
    @Damien_The_Unbeliever You missed one thing that applies to verbatim + interpolated... In the case of a verbatim interpolated string brace escape sequences ({{ and }}) are not interpreted literally; they produce single brace characters. – TheSoftwareJedi Aug 23 '18 at 18:28