简体   繁体   中英

Delete LF from file using C#

I have to make a script in C# to delete 'LF' from a file.

The structure of the file like this :

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

I want to have this :

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

Thank you !

If the file is not that large (ie we can read it into memory):

 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);

You can try using a Regex expression for this:

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

Hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM