[ACCEPTED]-C# @"" how do i insert a tab?-c#

Accepted answer
Score: 31

When you are using the @ modifier, you are 8 using something called a verbatim string literal.

What this means 7 is that anything you put in between the 6 Opening and closing quotes will be used 5 in the string.

This includes Carraige Return, Line 4 Feed, Tab and etc.

Short answer: Just press 3 tab.

One caveat, though. Your IDE may decide 2 to insert spaces instead of a tab character, so 1 you may be better off using concatenation.

Score: 20

None of the normal escape sequences work 7 in verbatim string literals (that's the 6 point!). If you want a tab in there, you'll 5 either have to put the actual tab character 4 in, or use string concatenation:

string x = @"some\stuff" + "\t" + @"some more stuff";

What are 3 you using a verbatim string literal for 2 in the first place? There may be a better 1 way of handling it.

Score: 9

That quote escape sequence ("") is the only "escape" that 8 works in verbatim string literals. All other escapes only work in regular 7 string literals.

As a workaround you may 6 use something ugly like this:

string.Format(@"Foo{0}Bar", "\t");

or include 5 an actual tab character in the string. That 4 should also work with regular string literals, but 3 whitespace, especially tabs, usually doesn't 2 survive different text editors well :-)

For 1 newlines it's arguably much easier:

Score: 3

Once you start a string with @ you can put 2 anything in between, also tabs or newlines:

string test = @"test



the 1 above string will contain the 4 newlines.

Score: 1

When using the @ syntax, all character escapes 7 are disabled. So to get a tab or a newline 6 you will have to insert a literal TAB or 5 a newline. It's easy - just hit the TAB 4 or ENTER button on your keyboard. Note, that 3 you might need to change the behavior of 2 TAB ir Visual Studio if it is set to enter 1 spaces instead of literal TAB characters.

Score: 1

The @ before a string assumes you have no 3 escaping, so you'll need to concatenate:

string value = @"Hello ""big""" +"\t"+ @"world \\\\\";

So 2 in other words, yes "" is the only trick 1 available.

Score: 1

If you are starting to need "special" characters 5 within your string then maybe you should 4 be using a normal string "" and escaping 3 them using \ rather than using the @"".

What 2 sort of data are you storing in the string? What 1 reason do you have for needing it in a @""?

Score: 1

I don't know why nobody suggested nowaday 3 to insert {"\t"} in your interpolation string, to 2 me it's better than having to use string 1 format.

this works: $@"Foo{"\t"}Bar"; or concatenating strings

Score: 0

Just write a tab or a newline in your string. @"" string 3 literals recognise every character as it 2 is written in the code file. On the contrary, escape 1 sequences don't work.

Score: 0

I would for sure not type a tab because 2 your editor may insert a few spaces instead. Using 1 \t is the better option.

Score: 0

The correctest way to do this:

string str = string.Format(
  CultureInfo.CurrentCulture,  // or InvariantCulture


string str = string.Format(
  CultureInfo.CurrentCulture,  // or InvariantCulture

you could 2 have more then one appearance of {0} in the 1 string.

Score: 0

Try this

var mystr = $@"before tab {"\t"} after tab";


More Related questions