简体   繁体   中英

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

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.

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";

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.

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.

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.

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 @""?

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

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.

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

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.

Try this

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

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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM