如何在C#控制台应用程序的字符串中插入选项卡?

时间:2020-12-26 20:35:37

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 /t some other string /t/t some 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.)

(我也对其他解决方案持开放态度。我可能必须使用|或其他一些分隔符/字符。)

4 个解决方案

#1


27  

Tabs in strings are typically written \t, not /t. Are you escaping your string correctly?

字符串中的制表符通常是\ t而不是/ t。你正确地逃脱了你的字符串吗?

#2


3  

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

#3


3  

Technically there is tab constant in .NET. It is in Microsoft.VisualBasic.dll.

从技术上讲,.NET中有tab常量。它位于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就可以得到相同的结果。

#4


2  

If you really feel disgusted by this \t question, why not write a simple utility class

如果你真的对这个问题感到厌恶,为什么不写一个简单的实用工具类

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

但是,为什么?,没有理由不使用预定义的标准转义序列

#1


27  

Tabs in strings are typically written \t, not /t. Are you escaping your string correctly?

字符串中的制表符通常是\ t而不是/ t。你正确地逃脱了你的字符串吗?

#2


3  

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

#3


3  

Technically there is tab constant in .NET. It is in Microsoft.VisualBasic.dll.

从技术上讲,.NET中有tab常量。它位于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就可以得到相同的结果。

#4


2  

If you really feel disgusted by this \t question, why not write a simple utility class

如果你真的对这个问题感到厌恶,为什么不写一个简单的实用工具类

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

但是,为什么?,没有理由不使用预定义的标准转义序列