C# @"" how do i insert a tab?

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

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

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

Short answer: Just press tab.

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


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

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

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


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

As a workaround you may use something ugly like this:

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

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

For newlines it's arguably much easier:

@"Foo
Bar";

Tags:

C#