Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

string

regex

How do you remove the \n, \t and spaces between strings in java?

like image 632
Praveen Avatar asked Nov 27 '22 08:11

Praveen


2 Answers

str.replaceAll("\\s+", "");

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

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

like image 147
Bozho Avatar answered Dec 05 '22 18:12

Bozho


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

s = s.replaceAll("\\s+", "");
like image 23
Mark Byers Avatar answered Dec 05 '22 18:12

Mark Byers