简体   繁体   中英

Write Tab Delimited txt file from c#.net

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

//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:

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();

Use \\t for the tab character. Using String.Format may present a more readable option:

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

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

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 :

use \\t not /t for tab in the string. so your string line should be:

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

if you do:

Console.WriteLine(line);

output would be:

FirstLine.      SecondLine.     ThirdLine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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