What is a quick way to force CRLF in C# / .NET?

前端 未结 6 1534
独厮守ぢ
独厮守ぢ 2020-12-08 13:38

How would you normalize all new-line sequences in a string to one type?

I\'m looking to make them all CRLF for the purpose of email (MIME documents). Ideally this w

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 14:04

    Simple variant:

    Regex.Replace(input, @"\r\n|\r|\n", "\r\n")
    

    For better performance:

    static Regex newline_pattern = new Regex(@"\r\n|\r|\n", RegexOptions.Compiled);
    [...]
        newline_pattern.Replace(input, "\r\n");
    

提交回复
热议问题