我下面這個article拆分文本與 ' r n'
,我想出了這個代碼:
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'? –
'Console.WriteLine()'會在數組中的每個字符串之後自動添加換行符。你想要在你的循環之前把這些行合併在一起,或者如你所建議的一個答案那樣,在'分割'文本之前替換'\ n'。 –
我想要具有以'\ r \ n'而不是'\ n'或'\ r'結尾的行。 – skmasq