Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep only number, letter and remove space

Tags:

java

regex

I try to keep only letter and number

String str = "it's too hard & jean say";
String strNew = str.replaceAll("[^A-Za-z0-9\\s]", "");

I tried with this code but space is not removed.

so i search a way to remove it.

like image 520
robert trudel Avatar asked Oct 23 '13 19:10

robert trudel


1 Answers

It does not remove spaces because you have specified to replace everything except for letters (A-Za-z), numbers (0-9), and spaces (\\s) with empty string.

Just remove the \\s and it should also replace spaces with empty string.

String strNew = str.replaceAll("[^A-Za-z0-9]", "");
like image 195
jbabey Avatar answered Oct 05 '22 04:10

jbabey