Hello everybody,
I have got lines in a text file, I want to read and load them to a listbox.
By default, the text file ends with a line feed ("\n\r"), so
foreach(string s in (List<string>)content.Replace("\r", "").Split(new char[] { '\n' }).ToList())
gives an empty line at the end.
What do you think is the most elegant way to remove it ?
I did this :
if(content.Substring(content.Length - 2, 2) == “\n\r” || content.Substring(content.Length - 2, 2) == “\r\n”)
{
content = content.Substring(0, content.Length - 2);
}
(sorry I fail to format this to code)
Do you want to preserve empty lines in the middle of the text?
If not: List<string> lines = content.Split ("\r\n".ToCharArray (), StringSplitOptions.RemoveEmptyEntries).ToList ();
Otherwise, to just remove some characters from the end:
content = content.TrimEnd("\r\n".ToCharArray());
You might also want to have a look at the File.ReadAllLines(filename)
function, see the documentation.
Hello,
Oh well I answered so quickly that I did not see you provided several solutions.
I think this could be quite OK. I shall test that, thank you.
Well, I am used to mark threads as solved once it is done, is there a button here for that ?