简体   繁体   English

C# @"" 如何插入标签?

[英]C# @"" how do i insert a tab?

Recently i found out i can write in a " by writing two " ex @"abc""def" .最近我发现我可以通过写两个" 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.我尝试搜索msdn ,但我找不到它。

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.这包括 Carraige Return、Line Feed、Tab 等。

Short answer: Just press tab.简短回答:只需按 Tab。

One caveat, though.不过,有一个警告。 Your IDE may decide to insert spaces instead of a tab character, so you may be better off using concatenation.您的 IDE 可能决定插入空格而不是制表符,因此最好使用串联。

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.上面的字符串将包含 4 个换行符。

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.这很简单 - 只需按键盘上的 TAB 或 ENTER 按钮即可。 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.请注意,如果将 TAB 设置为输入空格而不是文字 TAB 字符,则您可能需要在 Visual Studio 中更改 TAB 的行为。

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.我不知道为什么现在没有人建议在您的插值字符串中插入{"\\t"} ,对我来说这比必须使用字符串格式要好。

this works: $@"Foo{"\\t"}Bar";这有效: $@"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.使用 \\t 是更好的选择。

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

Use '\\\\t' to escape the first backslash, then it will be interpreted as a tab.使用 '\\\\t' 转义第一个反斜杠,然后它将被解释为制表符。 Sorry for the confusion...this site escaped the first slash...really when editing this there is three slashes....很抱歉造成混乱……这个网站逃脱了第一个斜线……编辑这个时真的有三个斜线……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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