从 c#.net 写入制表符分隔的 txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11661884/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:42:32  来源:igfitidea点击:

Write Tab Delimited txt file from c#.net

c#asp.net

提问by Steffano Soh

I'm having some problem writing a tab delimited string into a txt file.

我在将制表符分隔的字符串写入 txt 文件时遇到了一些问题。

//This is the result I want:    
First line. Second line.    nThird line.

//But I'm getting this:
First line./tSecond line./tThird line.

Below is my code where I pass the string to be written into the txt file:

下面是我传递要写入txt文件的字符串的代码:

string word1 = "FirstLine.";
string word2 = "SecondLine.";
string word3 = "ThirdLine.";
string line = word1 + "/t" + word2 + "/t" + word3;

System.IO.StreamWriter file = new System.IO.StreamWriter(fileName, true);
file.WriteLine(line);

file.Close();

采纳答案by dasblinkenlight

Use \tfor the tab character. Using String.Formatmay present a more readable option:

使用\t的制表符。使用String.Format可能会提供一个更具可读性的选项:

line = string.Format("{0}\t{1}\t{2}", word1, word2, word3);

回答by Habib

use \tnot /tfor tab in the string. so your string lineshould be:

使用\t/t为字符串中的选项卡。所以你的字符串line应该是:

string line = word1 + "\t" + word2 + "\t" + word3;

if you do:

如果你这样做:

Console.WriteLine(line);

output would be:

输出将是:

FirstLine.      SecondLine.     ThirdLine.

回答by Jonathon Reinhart

To write a tab character you need to use "\t". It's a backslash (above the enter key), not a forward slash.

要编写制表符,您需要使用"\t". 它是一个反斜杠(在回车键上方),而不是一个正斜杠。

So your code should read:

所以你的代码应该是:

string line = word1 + "\t" + word2 + "\t" + word3;

For what it's worth, here is a list of common "escape sequences" like "\t" = TAB:

对于它的价值,这里是一个常见的“转义序列”列表,如"\t" = TAB