如何将回车 returns 转换为实际的换行符
How to convert carriage returns into actual line breaks
我有一个从 this 下载的文本文件(它只是英语词典),它在浏览器中显示正常,但当我在记事本中打开它时,它无法识别换行符。我认为一个简单的 C# 应用程序可以检测到他们使用的回车 returns 的味道并将它们变成实际的换行符并吐出一个格式更好的 txt 文件但是我失败了 String.Replace("\r", "\n");
这样的技术我思想将是简单的技巧。这些回车 returns 是如何编码的,我如何重新格式化文件以使其在记事本之类的东西中可读? C# 是首选,因为这是我习惯的,但如果使用其他方法更容易,我会很乐意考虑替代方案。
记事本是我所知道的唯一 Windows 文本编辑器,它不识别 Unix 风格的换行符 \n
,并且需要 Windows 风格的换行符 \r\n
正确格式化文本。如果将 \n
转换为 \r\n
,它将按预期显示。此外,任何其他(现代)文本编辑器都应按原样正确显示文本。
如果您真的想在 C# 中执行此操作,您需要做的就是...
File.WriteAllLines("outfile.txt", File.ReadAllLines("infile.txt"));
...如果您想要稍微复杂一点但速度更快且内存更少的人,请这样做...
using (var reader = new StreamReader("infile.txt"))
using (var writer = new StreamWriter("outfile.txt"))
while (!reader.EndOfStream)
writer.WriteLine(reader.ReadLine());
...如果您真的想把它作为使用扩展方法和 LINQ 的借口,那么就这样做...
//Sample use
//"infile.txt".ReadFileAsLines()
// .WriteAsLinesTo("outfile.txt");
public static class ToolKit
{
public static IEnumerable<string> ReadFileAsLines(this string infile)
{
if (string.IsNullOrEmpty(infile))
throw new ArgumentNullException("infile");
if (!File.Exists(infile))
throw new FileNotFoundException("File Not Found", infile);
using (var reader = new StreamReader(infile))
while (!reader.EndOfStream)
yield return reader.ReadLine();
}
public static void WriteAsLinesTo(this IEnumerable<string> lines, string outfile)
{
if (lines == null)
throw new ArgumentNullException("lines");
if (string.IsNullOrEmpty(outfile))
throw new ArgumentNullException("outfile");
using (var writer = new StreamWriter(outfile))
foreach (var line in lines)
writer.WriteLine(line);
}
}
我有一个从 this 下载的文本文件(它只是英语词典),它在浏览器中显示正常,但当我在记事本中打开它时,它无法识别换行符。我认为一个简单的 C# 应用程序可以检测到他们使用的回车 returns 的味道并将它们变成实际的换行符并吐出一个格式更好的 txt 文件但是我失败了 String.Replace("\r", "\n");
这样的技术我思想将是简单的技巧。这些回车 returns 是如何编码的,我如何重新格式化文件以使其在记事本之类的东西中可读? C# 是首选,因为这是我习惯的,但如果使用其他方法更容易,我会很乐意考虑替代方案。
记事本是我所知道的唯一 Windows 文本编辑器,它不识别 Unix 风格的换行符 \n
,并且需要 Windows 风格的换行符 \r\n
正确格式化文本。如果将 \n
转换为 \r\n
,它将按预期显示。此外,任何其他(现代)文本编辑器都应按原样正确显示文本。
如果您真的想在 C# 中执行此操作,您需要做的就是...
File.WriteAllLines("outfile.txt", File.ReadAllLines("infile.txt"));
...如果您想要稍微复杂一点但速度更快且内存更少的人,请这样做...
using (var reader = new StreamReader("infile.txt"))
using (var writer = new StreamWriter("outfile.txt"))
while (!reader.EndOfStream)
writer.WriteLine(reader.ReadLine());
...如果您真的想把它作为使用扩展方法和 LINQ 的借口,那么就这样做...
//Sample use
//"infile.txt".ReadFileAsLines()
// .WriteAsLinesTo("outfile.txt");
public static class ToolKit
{
public static IEnumerable<string> ReadFileAsLines(this string infile)
{
if (string.IsNullOrEmpty(infile))
throw new ArgumentNullException("infile");
if (!File.Exists(infile))
throw new FileNotFoundException("File Not Found", infile);
using (var reader = new StreamReader(infile))
while (!reader.EndOfStream)
yield return reader.ReadLine();
}
public static void WriteAsLinesTo(this IEnumerable<string> lines, string outfile)
{
if (lines == null)
throw new ArgumentNullException("lines");
if (string.IsNullOrEmpty(outfile))
throw new ArgumentNullException("outfile");
using (var writer = new StreamWriter(outfile))
foreach (var line in lines)
writer.WriteLine(line);
}
}