我刚刚开始学习C#,现在我正在尝试使用File IO.我在向文件写一个tab(\t)字符时遇到了一些问题.
到目前为止这是我的代码:
static void Main(string[] args)
{
string[] input = Console.ReadLine().Split(' ');
Console.WriteLine(string.Join("\n", input));
File.WriteAllLines(@"C:\Users\Shashank\Desktop\test.txt", input);
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
当我运行脚本并输入此文本时:
hello \twhat \tis \tyour name
Run Code Online (Sandbox Code Playgroud)
以下内容将写入我的文件:
hello
\twhat
\tis
\tyour
name
Run Code Online (Sandbox Code Playgroud)
但是,我希望文件输出看起来像:
hello
what
is
your
name
Run Code Online (Sandbox Code Playgroud)
我已经在线查看但找不到能给我预期结果的解决方案.我也尝试使用StreamWriter但无济于事.
"\t"只有在字符串常量中使用时才表示制表符.当它只是开关输入字符串-没有什么可以解释两个字符\,并t以任何特殊的方式(同为其他转义序列).
您可以使用String.Replace自行替换它们:
string[] input = Console.ReadLine().Replace(@"\t", "\t").Split(' ')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
340 次 |
| 最近记录: |