如何从C#中的字符串末尾删除新行? [英] How do I remove the new line from the end of string in C#?
本文介绍了如何从C#中的字符串末尾删除新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从字符串末尾删除新行?这是我用二进制文件写入后对记录进行排序的代码
我有什么试过:
how to remove the new line from the end of string ?This is my code to sort the records after writing it with binarywriter
What I have tried:
string inFile = Class1.filename;
string outFile = Class1.filename;
var contents = File.ReadAllLines(inFile);
Array.Sort(contents);
File.WriteAllLines(outFile, contents);
推荐答案
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));
这篇关于如何从C#中的字符串末尾删除新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文