How to remove empty lines in a string in C#? I am generating some text files in C# (winforms) and for some reason there are some empty lines. How can I remove them after the
private string remove_space(string st) { String final = ""; char[] b = new char[] { '\r', '\n' }; String[] lines = st.Split(b, StringSplitOptions.RemoveEmptyEntries); foreach (String s in lines) { if (!String.IsNullOrWhiteSpace(s)) { final += s; final += Environment.NewLine; } } return final; }