Java Regex remove new lines, but keep spaces.

后端 未结 5 1350
名媛妹妹
名媛妹妹 2021-01-18 10:21

For the string \" \\n a b c \\n 1 2 3 \\n x y z \" I need it to become \"a b c 1 2 3 x y z\".

Using this regex str.replaceAll(\"(\\s|

5条回答
  •  忘掉有多难
    2021-01-18 10:57

    You don't have to use regex; you can use trim() and replaceAll() instead.

     String str = " \n a b c \n 1 2 3 \n x y z ";
     str = str.trim().replaceAll("\n ", "");
    

    This will give you the string that you're looking for.

提交回复
热议问题