To remove special characters from a string in Java, use the
replaceAll
method with [^\\w\\s]
regular expression pattern.Here's how you do it:
String string = "Hello World 123!,.($#)&@";
String noSpecials = string.replaceAll("[^\\w\\s]", "");
System.out.println(noSpecials); // Hello World 123