views:

76

answers:

2

how to remove the \n, \t and spaces between the strings in java?

+3  A: 

Use String.replaceAll with a regular expression to remove all whitespace:

s = s.replaceAll("\\s+", "");
Mark Byers
Ah, come on, Mark, you even go as far as removing the fun of those annoying RTFM questions.
Riduidel
+6  A: 
str.replaceAll("\\s+", "");

\s A whitespace character: [ \t\n\x0B\f\r]

java.util.regex.Pattern has all the predefined classes.

Bozho
how to remove only \n \t not spaces
Praveen Chandrasekaran
the same way, but using `\\n+\\t+` instead
Bozho
But note that `\\n+\\t+` only matches new lines followed by tabs. Probably `[\t\n]+` is meant...
Bart Kiers
yes, thanks. my bad.
Bozho