How to remove empty lines from a formatted string?

后端 未结 11 1535
一生所求
一生所求 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:50

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

提交回复
热议问题