Replace Line Breaks in a String C#

后端 未结 17 1107
失恋的感觉
失恋的感觉 2020-11-22 11:16

How can I replace Line Breaks within a string in C#?

17条回答
  •  不知归路
    2020-11-22 11:28

    I needed to replace the \r\n with an actual carriage return and line feed and replace \t with an actual tab. So I came up with the following:

    public string Transform(string data)
    {
        string result = data;
        char cr = (char)13;
        char lf = (char)10;
        char tab = (char)9;
    
        result = result.Replace("\\r", cr.ToString());
        result = result.Replace("\\n", lf.ToString());
        result = result.Replace("\\t", tab.ToString());
    
        return result;
    }
    

提交回复
热议问题