[ACCEPTED]-Text cleaning and replacement: delete \n from a text in Java-string

Accepted answer
Score: 43

Hooknc is right. I'd just like to post a 19 little explanation:

"\\n" translates to "\n" after 18 the compiler is done (since you escape the 17 backslash). So the regex engine sees "\n" and 16 thinks new line, and would remove those 15 (and not the literal "\n" you have).

"\n" translates 14 to a real new line by the compiler. So the 13 new line character is send to the regex 12 engine.

"\\\\n" is ugly, but right. The compiler 11 removes the escape sequences, so the regex 10 engine sees "\\n". The regex engine sees 9 the two backslashes and knows that the first 8 one escapes it so that translates to checking 7 for the literal characters '\' and 'n', giving 6 you the desired result.

Java is nice (it's 5 the language I work in) but having to think 4 to basically double-escape regexes can be 3 a real challenge. For extra fun, it seems 2 StackOverflow likes to try to translate 1 backslashes too.

Score: 22

I think you need to add a couple more slashies...

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

Explanation: The 19 number of slashies has to do with the fact 18 that "\n" by itself is a controlled character 17 in Java.

So to get the real characters of 16 "\n" somewhere we need to use "\n". Which 15 if printed out with give us: "\"

You're looking 14 to replace all "\n" in your file. But you're 13 not looking to replace the control "\n". So 12 you tried "\n" which will be converted into 11 the characters "\n". Great, but maybe not 10 so much. My guess is that the replaceAll 9 method will actually create a Regular Expression 8 now using the "\n" characters which will 7 be misread as the control character "\n".

Whew, almost 6 done.

Using replaceAll("\\n", "") will first 5 convert "\\n" -> "\n" which will be used 4 by the Regular Expression. The "\n" will 3 then be used in the Regular Expression and 2 actually represents your text of "\n". Which 1 is what you're looking to replace.

Score: 11

Instead of String.replaceAll(), which uses 4 regular expressions, you might be better 3 off using String.replace(), which does simple 2 string substitution (if you are using at 1 least Java 1.5).

String replacement = string.replace("\\n", "");

should do what you want.

Score: 3
string = string.replaceAll(""+(char)10, " ");


Score: 2

Try this. Hope it helps.

raw = raw.replaceAll("\t", "");
raw = raw.replaceAll("\n", "");
raw = raw.replaceAll("\r", "");


Score: 1

The other answers have sufficiently covered 14 how to do this with replaceAll, and how you need to 13 escape backslashes as necessary.

Since 1.5., there 12 is also String.replace(CharSequence, CharSequence) that performs literal string replacement. This 11 can greatly simplify many problem of string 10 replacements, because there is no need to 9 escape any regular expression metacharacters 8 like ., *, |, and yes, \ itself.

Thus, given 7 a string that can contain the substring 6 "\n" (not '\n'), we can delete them as follows:

String before = "Hi!\\n How are you?\\n I'm \n   good!";
// Hi!\n How are you?\n I'm 
//   good!

String after = before.replace("\\n", "");

// Hi! How are you? I'm 
//   good!

Note 5 that if you insist on using replaceAll, you can prevent 4 the ugliness by using Pattern.quote:

    before.replaceAll(Pattern.quote("\\n"), "")
// Hi! How are you? I'm 
//   good!

You should also use 3 Pattern.quote when you're given an arbitrary string that 2 must be matched literally instead of as 1 a regular expression pattern.

Score: 1

I used this solution to solve that problem:

String replacement = str.replaceAll("[\n\r]", "");


Score: 0

Normally \n works fine. Otherwise you can 4 opt for multiple replaceAll statements. first 3 apply one replaceAll on the text, and then 2 reapply replaceAll again on the text. Should 1 do what you are looking for.

More Related questions