2012-07-26 12 views
5

Sto riscontrando qualche problema nella scrittura di una stringa delimitata da tabulazioni in un file txt.Scrittura scheda delimitato da file txt da C# .net

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

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

Qui di seguito è il mio codice in cui passo la stringa da scrivere nel 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(); 

risposta

15

Usa \t per il carattere tab. L'utilizzo di String.Format può presentare un'opzione più leggibile:

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

uso \t non /t per scheda nella stringa. quindi la stringa line dovrebbe essere:

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

se lo fai:

Console.WriteLine(line); 

uscita potrebbe essere:

FirstLine.  SecondLine.  ThirdLine. 
+0

No .. utilizzare "\ t". "\\ t" sfugge alla barra ... –

+0

@EricJ., assolutamente giusto. Non so cosa stavo pensando – Habib

4

Per scrivere un carattere di tabulazione è necessario utilizzare "\t". È una barra rovesciata (sopra il tasto Invio), non una barra in avanti.

Quindi il codice dovrebbe leggere:

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

Per quello che vale, ecco un elenco di comuni "sequenze di escape" come "\t" = TAB:

Problemi correlati