我遵循了这篇文章
然后我写出了这段代码:
string FileName = "C:\\test.txt";
using (StreamReader sr = new StreamReader(FileName, Encoding.Default))
{
string[] stringSeparators = new string[] { "\r\n" };
string text = sr.ReadToEnd();
string[] lines = text.Split(stringSeparators, StringSplitOptions.None);
foreach (string s in lines)
{
Console.WriteLine(s);
}
}
这是示例文本:
somet interesting text\n
some text that should be in the same line\r\n
some text should be in another line
这里是输出结果:
somet interesting text\r\n
some text that should be in the same line\r\n
some text should be in another line\r\n
但我想要的是这个:
somet interesting textsome text that should be in the same line\r\n
some text should be in another line\r\n
我认为我应该得到这个结果,但不知道为什么有些东西缺失了...
File.ReadAllLines
或File.ReadLines
? - Tim SchmelterConsole.WriteLine()
会在你的数组中每个字符串后自动添加换行符。你需要在循环之前将这些行合并,或者像一个答案建议的那样,在Split
文本之前替换掉\n
。 - dub stylee\r\n
结尾,而不是\n
或\r
。 - skmasq\r\n
和\n
,而是你忘记了这些行已经包含了换行符。因此,即使你将它们删除,文件中仍然有三行。 - Tim SchmelterConsole.WriteLine("lines.Length={0}", lines.Length);
- Austin Salonen