How to remove empty lines from a formatted string?

后端 未结 11 1530
一生所求
一生所求 2020-12-01 02:24

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

11条回答
  •  無奈伤痛
    2020-12-01 02:33

    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();
    }
    

提交回复
热议问题