26

Recently i found out i can write in a " by writing two " ex @"abc""def". I find the @ string literal useful. But how do i write in a tab or newline? is "" the only trick available? I tried search msdn but i couldnt find it.

13 Answers 13

32

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.

2
  • d'oh, press the tab key. How silly of me. I'll pay attention to the IDE.
    – user34537
    Nov 17, 2009 at 14:02
  • 9
    Keep in mind that tabs might not survive some tools grinding over your code. Using literal tabs in literals is rare and that might be the assumption of whoever wrote what changes them blindly to spaces, too :-)
    – Joey
    Nov 17, 2009 at 14:07
21

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.

3
  • Why the hell did I think of string.Format and not of normal string concatenation? Ouch.
    – Joey
    Nov 17, 2009 at 13:57
  • 1
    Doesn't string addition conflict with interning working correctly? Nov 17, 2009 at 16:03
  • Maybe because there seems to be a bias towards format-style examples for C# - in books and on the web. Personally, I prefer WriteLine("Name=" + nameVar); for simple formatting applications. Nov 17, 2009 at 16:10
9

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";
2
  • This is how I would do it, I always use format string for any where that merges any text together. Nov 17, 2009 at 16:16
  • For concatenating strings there are nicer options, though :-). I just totally forgot about string.Concat (which gets used behind the scenes by the + operator, too)
    – Joey
    Nov 17, 2009 at 16:45
3

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

string test = @"test

bla

bjkl";

the above string will contain the 4 newlines.

1

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

0
1

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

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

1

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

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

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

1
  • 1
    Always remember to put the @ in front of the second string as well. @"Hello big" + "\t" + @"world";
    – Robin Day
    Nov 17, 2009 at 17:20
1

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

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

0

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

0

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

0

The correctest way to do this:

string str = string.Format(
  CultureInfo.CurrentCulture,  // or InvariantCulture
  @"abc{0}cde",
  "\t");

and

string str = string.Format(
  CultureInfo.CurrentCulture,  // or InvariantCulture
  @"abc{0}cde",
  Environment.NewLine);

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

0

Try this

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

Use '\\t' to escape the first backslash, then it will be interpreted as a tab. Sorry for the confusion...this site escaped the first slash...really when editing this there is three slashes....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.