replace \n and \r\n with
in java

后端 未结 7 1501
余生分开走
余生分开走 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:30

    This should work. You need to put in two slashes

    str = str.replaceAll("(\\r\\n|\\n)", "
    ");

    In this Reference, there is an example which shows

    private final String REGEX = "\\d"; // a single digit
    

    I have used two slashes in many of my projects and it seems to work fine!

提交回复
热议问题