0

我必须在 C# 中编写一个脚本才能从文件中删除“LF”。

文件结构如下:

line1 'CR''LF'
line2 'CR''LF'
line3 'LF'
some_text_of_line_3 'CR''LF'

我想要这个:

line1 'CR'
line2 'CR'
line3 some_text_of_line_3 'CR'

谢谢 !

4

2 回答 2

2

如果文件不是那么(即我们可以将其读入内存):

 using System.IO;

 ...

 String path = @"c:\MyFile.txt";

 // Read file, remove LF (which is "\n")
 string text = File.ReadAllText(path).Replace("\n", ""); 

 // Write text back
 File.WriteAllText(path, text);
于 2019-08-01T13:06:22.620 回答
1

您可以尝试为此使用正则表达式:

string myFile= File.ReadAllText(@"c:\FileName.txt");
myFile= Regex.Replace(myFile, @"LF", "AnythingYouwantHere");
File.WriteAllText(@"c:\FileName.txt", myFile);

希望这对您有所帮助。

于 2019-08-01T13:01:06.150 回答