C# 使用'拆分文本\r\n';
我一直在关注这个 我想出了这个密码:C# 使用'拆分文本\r\n';,c#,string,split,C#,String,Split,我一直在关注这个 我想出了这个密码: 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(
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
我想我应该得到这个结果,但不知何故,我遗漏了一些东西…使用静态
文件
类读取文件更容易:
// First read all the text into a single string.
string text = File.ReadAllText(FileName);
// Then split the lines at "\r\n".
string[] stringSeparators = new string[] { "\r\n" };
string[] lines = text.Split(stringSeparators, StringSplitOptions.None);
// Finally replace lonely '\r' and '\n' by whitespaces in each line.
foreach (string s in lines) {
Console.WriteLine(s.Replace('\r', ' ').Replace('\n', ' '));
}
注意:文本还可能包含垂直制表器\v
。这些由Microsoft Word用作手动换行符
为了捕捉任何可能的中断,可以使用regex进行替换
Console.WriteLine(Regex.Replace(s, @"[\f\n\r\t\v]", " "));
我想问题出在你的文本文件里。它可能已经分成了太多行,当您阅读它时,它会“添加”额外的
\r
和/或\n
字符(正如它们存在于文件中一样)。检查读入文本的内容
变量
下面的代码(在包含文本的局部变量上)运行良好,可拆分为两行:
string[] stringSeparators = new string[] { "\r\n" };
string text = "somet interesting text\nsome text that should be in the same line\r\nsome text should be in another line";
string[] lines = text.Split(stringSeparators, StringSplitOptions.None);
这对我有用
string stringSeparators = "\r\n";
string text = sr.ReadToEnd();
string lines = text.Replace(stringSeparators, "");
lines = lines.Replace("\\r\\n", "\r\n");
Console.WriteLine(lines);
using System.IO;
//
string readStr = File.ReadAllText(file.FullName);
string[] read = readStr.Split(new char[] {'\r','\n'},StringSplitOptions.RemoveEmptyEntries);
第一个替换替换文本文件新行中的
\r\n
,第二个替换读取文件时转换为\\r\\n
的实际\r\n
文本。(当读取文件时,\
变为\
)。问题不在于拆分,而在于写入线。用WriteLine
打印的字符串中的\n
将产生一个“额外”行
范例
var text =
"somet interesting text\n" +
"some text that should be in the same line\r\n" +
"some text should be in another line";
string[] stringSeparators = new string[] { "\r\n" };
string[] lines = text.Split(stringSeparators, StringSplitOptions.None);
Console.WriteLine("Nr. Of items in list: " + lines.Length); // 2 lines
foreach (string s in lines)
{
Console.WriteLine(s); //But will print 3 lines in total.
}
要解决此问题,请在打印字符串之前删除\n
Console.WriteLine(s.Replace("\n", ""));
我采用了一种更紧凑的方法,将一个文本区域的输入分割成一个字符串列表。如果适合你的目的,你可以使用这个
问题是您不能按\r\n拆分,因此我事先删除了\r\n,并仅按\r\n拆分
var serials = model.List.Replace("\n","").Split('\r').ToList<string>();
var serials=model.List.Replace(“\n”和“).Split(“\r”).ToList();
我喜欢这种方法,因为你只需要一行就可以做到 这对我很有效
string stringSeparators = "\r\n";
string text = sr.ReadToEnd();
string lines = text.Replace(stringSeparators, "");
lines = lines.Replace("\\r\\n", "\r\n");
Console.WriteLine(lines);
using System.IO;
//
string readStr = File.ReadAllText(file.FullName);
string[] read = readStr.Split(new char[] {'\r','\n'},StringSplitOptions.RemoveEmptyEntries);
下面的代码给出了预期的结果
string text=“一些有趣的文本\n一些应该在同一行中的文本\r\n一些
文本应位于另一行“
var results=text.Split(新[]{“\n”,“\r\n”},StringSplitOptions.None);
在Winform应用程序中(C):
static string strFilesLoc = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), @"..\..\")) + "Resources\\";
public static string[] GetFontFamily()
{
var result = File.ReadAllText(strFilesLoc + "FontFamily.txt").Trim();
string[] items = result.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
return items;
}
文本文件(FontFamily.txt)中的:
Microsoft无衬线
9
true
为什么不使用File.ReadAllLines
或File.ReadLines
?控制台。WriteLine()
将在数组中的每个字符串后自动添加换行符。您希望在循环之前将这些行合并在一起,或者按照一个答案的建议,在拆分文本之前,请先替换\n
。我希望有一行特别以\r\n
结尾,而不是\n
或\r
。问题不是结尾处有\r\n
和\n
,而是您忘记了这些行实际上已经包含换行符。因此,即使删除它们,文件中仍有三行。是否确定输出的第一行以\r\n
结尾?添加这一行,看看它是否符合您的期望:Console.WriteLine(“lines.Length={0}”,lines.Length)
但我为什么要添加它?是的,控制台.WriteLine()正在将\n
转换为\r\n
。