How to eliminate ALL line breaks in string?

后端 未结 12 1785
傲寒
傲寒 2021-01-30 10:22

I have a need to get rid of all line breaks that appear in my strings (coming from db). I do it using code below:

value.Replace(\"\\r\\n\", \"\").Replace(\"\\n\"         


        
12条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-30 11:13

    Below is the extension method solving my problem. LineSeparator and ParagraphEnding can be of course defined somewhere else, as static values etc.

    public static string RemoveLineEndings(this string value)
    {
        if(String.IsNullOrEmpty(value))
        {
            return value;
        }
        string lineSeparator = ((char) 0x2028).ToString();
        string paragraphSeparator = ((char)0x2029).ToString();
    
        return value.Replace("\r\n", string.Empty)
                    .Replace("\n", string.Empty)
                    .Replace("\r", string.Empty)
                    .Replace(lineSeparator, string.Empty)
                    .Replace(paragraphSeparator, string.Empty);
    }
    

提交回复
热议问题