Reputation:
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.
Upvotes: 27
Views: 24264
Reputation: 804
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
Upvotes: 1
Reputation: 65436
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.
Upvotes: 1
Reputation: 34592
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....
Upvotes: -1
Reputation: 64628
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.
Upvotes: 0
Reputation: 354576
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";
Upvotes: 9
Reputation: 6388
I would for sure not type a tab because your editor may insert a few spaces instead. Using \t is the better option.
Upvotes: 0
Reputation: 11357
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.
Upvotes: 0
Reputation: 8372
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.
Upvotes: 3
Reputation: 102478
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 @""?
Upvotes: 1
Reputation: 106920
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.
Upvotes: 1
Reputation: 49544
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.
Upvotes: 32
Reputation: 1500825
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.
Upvotes: 21