Replace new line/return with space using regex

前端 未结 7 1776
醉梦人生
醉梦人生 2020-12-24 06:36

Pretty basic question for someone who knows.

Instead of getting from

\"This is my text. 

And here is a ne         


        
7条回答
  •  独厮守ぢ
    2020-12-24 06:59

    I found this.

    String newString = string.replaceAll("\n", " ");
    

    Although, as you have a double line, you will get a double space. I guess you could then do another replace all to replace double spaces with a single one.

    If that doesn't work try doing:

    string.replaceAll(System.getProperty("line.separator"), " ");
    

    If I create lines in "string" by using "\n" I had to use "\n" in the regex. If I used System.getProperty() I had to use that.

提交回复
热议问题