C# @“” how do i insert a tab?

后端 未结 12 1113
南笙
南笙 2021-01-17 08:02

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 n

相关标签:
12条回答
  • 2021-01-17 08:13

    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.

    0 讨论(0)
  • 2021-01-17 08:15

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

    0 讨论(0)
  • 2021-01-17 08:19

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

    0 讨论(0)
  • 2021-01-17 08:20

    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.

    0 讨论(0)
  • 2021-01-17 08:20

    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 讨论(0)
  • 2021-01-17 08:22

    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 讨论(0)
提交回复
热议问题