且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从C#中的字符串末尾删除新行?

更新时间:2022-05-21 07:08:04

ReadAllLines会删除所有换行符,但WriteAllLines会将它们全部收回!

而是试试这个:

ReadAllLines does remove all newlines, but WriteAllLines puts them all back!
Instead try this:
string inFile = Class1.filename;
string outFile = Class1.filename;
var contents = File.ReadAllLines(inFile);
Array.Sort(contents);      
File.WriteAllText(outFile, string.Concat(contents));