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 static string RemoveEmptyLines(string text) { var lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); var sb = new StringBuilder(text.Length); foreach (var line in lines) { sb.AppendLine(line); } return sb.ToString(); }