如何在 C# 控制台应用程序的字符串中插入制表符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14184693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I insert tabs into strings for a C# console application?
提问by Thomas
This console application will write .txt files to disc.
此控制台应用程序会将 .txt 文件写入光盘。
User wants to import these .txt files into Excel such that they are formatted correctly, so I plan to use tabs.
用户希望将这些 .txt 文件导入 Excel 以使其格式正确,因此我计划使用选项卡。
I keep getting this nonsense "Some string/tsome other string/t/tsome other string". There is no Environment.Tab like there is Environment.NewLine.
我不断收到这种废话“一些字符串/t一些其他字符串/t/t一些其他字符串”。没有 Environment.Tab 就像有 Environment.NewLine。
How do I get the tabs and not /t into my strings?
如何将制表符而不是 /t 放入我的字符串中?
I'm sure there's a way and it'll probably be so obvious that I'll have to call myself faint all day on response.
我敢肯定有一种方法,而且它可能非常明显,以至于我不得不整天称自己昏昏沉沉。
(I'm open to other solutions as well. I might have to use | or some other delimiter/character.)
(我也愿意接受其他解决方案。我可能必须使用 | 或其他一些分隔符/字符。)
采纳答案by Dan Puzey
Tabs in strings are typically written \t
, not /t
. Are you escaping your string correctly?
字符串中的制表符通常是写的\t
,而不是/t
. 你是否正确地转义了你的字符串?
回答by hmatar
If the purpose is to create text files which will eventually be imported int excel why don't you use comma separated values. More info http://en.wikipedia.org/wiki/Comma-separated_values
如果目的是创建最终将导入 int excel 的文本文件,为什么不使用逗号分隔值。更多信息 http://en.wikipedia.org/wiki/Comma-separated_values
回答by Vlad Bezden
Technically there is tab constant in .NET. It is in Microsoft.VisualBasic.dll.
从技术上讲,.NET 中有制表符常量。它位于 Microsoft.VisualBasic.dll 中。
var tab = Microsoft.VisualBasic.Constants.vbTab;
But there is no reason to use vbTab, you can just use \t with the same result.
但是没有理由使用 vbTab,您可以使用 \t 获得相同的结果。
回答by Steve
If you really feel disgusted by this \t
question, why not write a simple utility class
如果你真的对这个\t
问题感到反感,为什么不写一个简单的实用程序类
public static class MyUtility
{
public static string Tab
{
get{return "\t";}
}
}
now you can use in your code to build your tab separated strings....
现在您可以在代码中使用来构建制表符分隔的字符串....
string test = "MyFirstString" + MyUtility.Tab + "MySecondString" + MyUtility.Tab .......
but, again, WHY?, there is no reason to not use the predefined standard escape sequence
但是,为什么?,没有理由不使用预定义的标准转义序列