2012-07-26 2 views
5

탭으로 구분 된 문자열을 txt 파일에 쓰는 데 문제가 있습니다. 문자열에서 탭탭으로 구분 된 txt 파일을 C# .net에서

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

답변

15

사용 \t : 그것은 가치가 무엇인지에 대한

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

, 여기 "\t" = TAB 같은 일반적인 "이스케이프 시퀀스"의 목록입니다 탭 문자. String.Format를 사용하면 더 읽기 옵션을 제공 할 수 있습니다

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

사용 \t하지 /t :

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

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

다음은 내가 문자열이 txt 파일에 기록 할 통과 내 코드입니다. 그래서 당신의 문자열 line은 다음과 같아야합니다

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

을 당신이 할 경우 :

Console.WriteLine(line); 

출력은 다음과 같습니다

FirstLine.  SecondLine.  ThirdLine. 
+0

아니 .. 사용 "\의 t". "\\ t"는 슬래시를 이스케이프합니다. –

+0

@EricJ., 절대적으로 맞습니다. 내가 뭘 생각했는지 모르겠다. – Habib

4

"\t"를 사용하는 데 필요한 탭 문자를 작성하려면. 이것은 백 슬래시 (Enter 키 위)입니다. 슬래시는 아닙니다.

그래서 코드를 읽어야

관련 문제