replace \n and \r\n with
in java

后端 未结 7 1463
余生分开走
余生分开走 2020-11-29 04:49

This has been asked several times for several languages but I can\'t get it to work. I have a string like this

String str = \"This is a string.\\nThis is a l         


        
7条回答
  •  独厮守ぢ
    2020-11-29 05:29

    It works for me.

    public class Program
    {
        public static void main(String[] args) {
            String str = "This is a string.\nThis is a long string.";
            str = str.replaceAll("(\r\n|\n)", "
    "); System.out.println(str); } }

    Result:

    This is a string.
    This is a long string.

    Your problem is somewhere else.

提交回复
热议问题