40

I have a string and would like to simply replace all of the newlines in it with the string " --linebreak-- ".

Would it be enough to just write:

string = string.replaceAll("\n", " --linebreak-- ");

I'm confused with the regex part of it. Do I need two slashes for the newline? Is this good enough?

Bohemian
  • 365,064
  • 84
  • 522
  • 658
Tim
  • 4,015
  • 8
  • 32
  • 47
  • 1
    Either works. See the answers to [this question](http://stackoverflow.com/questions/9113328/java-regular-expression-need-to-escape-backslash-in-regex) for a great explanation. – Kevin K Mar 24 '12 at 04:06

7 Answers7

65

Don't use regex!. You only need a plain-text match to replace "\n".

Use replace() to replace a literal string with another:

string = string.replace("\n", " --linebreak-- ");

Note that replace() still replaces all occurrences, as does replaceAll() - the difference is that replaceAll() uses regex to search.

Bohemian
  • 365,064
  • 84
  • 522
  • 658
  • 2
    So are you saying to use replace() inside a loop instead of using replaceAll() once? I dont understand why that's a better idea? – Tim Mar 24 '12 at 19:48
  • 7
    @Tim Calling `replace()` *once* replaces *all* occurrences (no "loop" required) – Bohemian Mar 24 '12 at 23:40
  • Oh wow. Thank you, for some reason I overlooked that when reading the spec. – Tim Mar 25 '12 at 15:50
  • @Bohemian Not sure if that's totally true--in a string with a bunch of multiple-spaces ( " " ), doing replaceAll("\\s+"," ") will reduce all multi-space sections down to one, but simply replace(...) will only do it once. – Amalgovinus May 07 '15 at 21:24
  • 7
    @Amalgovinus `replace()` replaces them all. The problem is that `replaceAll()` is badly named: Both methods replace all occurrences - the only difference between them is that `replaceAll()` uses *regex* for finding matches, while replace uses plain text. – Bohemian May 07 '15 at 23:05
  • I use `string = string.replace("\\n", System.lineSeparator());` Edit: But that is when I'm passing 'new line characters' in from an external source (like from the command line).. so the above code replaces the literal '/n' with an actual 'new line', you might have been trying to do the reverse. – Matt Sep 13 '18 at 16:26
40

Use below regex:

 s.replaceAll("\\r?\\n", " --linebreak-- ")

There's only really two newlines for UNIX and Windows OS.

kandarp
  • 4,619
  • 11
  • 31
  • 43
33

Since Java 8 regex engine supports \R which represents any line separator (little more info: https://stackoverflow.com/a/31060125/1393766).

So if you have access to Java 8 you can use

string = string.replaceAll("\\R", " --linebreak-- ");
Pshemo
  • 113,402
  • 22
  • 170
  • 242
4

No need for 2 backslashes.

 String string = "hello \n world" ;
 String str = string.replaceAll("\n", " --linebreak-- ");
 System.out.println(str);

Output = hello --linebreak-- world

RanRag
  • 43,987
  • 34
  • 102
  • 155
2

Just adding this for completeness, because the 2 backslash thing is real.

Refer to @dasblinkenlight answer in the following SO question (talking about \t but it applies for \n as well):

java, regular expression, need to escape backslash in regex

"There are two interpretations of escape sequences going on: first by the Java compiler, and then by the regexp engine. When Java compiler sees two slashes, it replaces them with a single slash. When there is t following a slash, Java replaces it with a tab; when there is a t following a double-slash, Java leaves it alone. However, because two slashes have been replaced by a single slash, regexp engine sees \t, and interprets it as a tab."

Community
  • 1
  • 1
Matt
  • 3,397
  • 1
  • 12
  • 22
  • The other thing that is happening is that the first arg to replaceAll can be a flat string or a regex. In a String the "\n" is interpreted as a literal, but if you include other regex only symbols, like brackets for char sets, then you need the extra slash in order to get correct string to the regex compiler, as you say......eg, "[\\r\\n]+" – Rondo Dec 18 '15 at 00:41
1

for new line there is a property

System.getProperty("line.separator")

Here as for your example,

string.replaceAll("\n", System.getProperty("line.separator"));
JavaLearner
  • 230
  • 1
  • 10
1

Looks good. You don't want 2 backslashes.

http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#sum

mikey
  • 4,858
  • 3
  • 21
  • 27