如何在“@...”内写入制表符 (\t) C# 字符串?

发布于 2024-12-04 04:12:38 字数 448 浏览 2 评论 0原文

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

I'm trying to just use the tab on my keyboard but the compiler interprets the tabs as spaces. Using \t won't work either, it will interpret it as \t literally. Is it not possible or am I missing something?

string str = @"\thi";
MessageBox.Show(str); // Shows "\thi"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

子栖 2024-12-11 04:12:38

分割你的字符串并在你想要的地方插入一个 \t

var str = @"This is a" + "\t" + @"tab";

Split your string and insert a \t where you want it?

var str = @"This is a" + "\t" + @"tab";
从来不烧饼 2024-12-11 04:12:38

逐字字符串文字 是关闭转义,以便可以在写入反斜杠时读取它们。如果您想转义,请使用常规字符串文字(不带 at 符号)。

当然,您可以在字符串中放置一个文本制表符(通过按 Tab 键)。

The whole point of a verbatim string literal is that escaping is turned off such that backslashes can be read as they are written. If you want escaping, then use a regular string literal (without the at symbol).

You could, of course, put a literal tab character (by pressing the tab key) within the string.

记忆で 2024-12-11 04:12:38

另一种选择是将制表符指定为 string.Format 中的参数:

string.Format(@"XX{0}XX", "\t"); // yields "XX    XX"

Another option is to specify the tab as a parameter in string.Format:

string.Format(@"XX{0}XX", "\t"); // yields "XX    XX"
满意归宿 2024-12-11 04:12:38

在 VS2010 中 - 如果您从富文本应用程序复制到剪贴板,并且该内容中有一个选项卡,我相信它将粘贴到 VS2010 编辑器中。

(我认为这有利于越野车,如果未来行为发生变化,我不会感到惊讶)

in VS2010 - if you copy to clipboard from a richtext application, and that content has a tab in it, I believe it will paste into the VS2010 editor as such.

(i consider this on the side of buggy and wouldn't be surprised if the behavior changes in the future)

薯片软お妹 2024-12-11 04:12:38

您正在使用字符串文字

相反,只需这样做:

string str = "\thi";
MessageBox.Show(str);

You are using a string literal.

Instead, just do this:

string str = "\thi";
MessageBox.Show(str);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文