我在将制表符分隔的字符串写入txt文件时遇到了一些问题.
//This is the result I want:
First line. Second line. nThird line.
//But I'm getting this:
First line./tSecond line./tThird line.
Run Code Online (Sandbox Code Playgroud)
下面是我传递要写入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();
Run Code Online (Sandbox Code Playgroud)
das*_*ght 16
使用\t的制表符.使用String.Format可能会提供更可读的选项:
line = string.Format("{0}\t{1}\t{2}", word1, word2, word3);
Run Code Online (Sandbox Code Playgroud)
要编写制表符,您需要使用"\t". 它是一个反斜杠(在回车键上方),而不是一个正斜杠。
所以你的代码应该是:
string line = word1 + "\t" + word2 + "\t" + word3;
Run Code Online (Sandbox Code Playgroud)
对于它的价值,这里是一个常见的“转义序列”列表,如"\t" = TAB:
| 归档时间: |
|
| 查看次数: |
27085 次 |
| 最近记录: |