Write Tab. Split txt file from C # .net

I had a problem writing a tab delimited string to 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 line to be written to 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();
+5
source share
3 answers

Use a character \tfor a tab character. Use String.Formatmay provide a more readable parameter:

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

To write a tab character you need to use "\t". This is a backslash (above the enter key), not a 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" such as "\t" = TAB:

+4
source

\t not /t . line :

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

:

Console.WriteLine(line);

:

FirstLine.      SecondLine.     ThirdLine.
0

All Articles