public static final String specialChars1= "\\W\\S";
String str2 = str1.replaceAll(specialChars1, "").replace(" ", "+");
public static final String specialChars2 = "`~!@#$%^&*()_+[]\\;\',./{}|:\"<>?";
String str2 = str1.replaceAll(specialChars2, "").replace(" ", "+");
No matter what str1, I want all characters other than letters and numbers to be removed and spaces to be replaced by a plus sign ( +).
My problem is that if I use specialChar1, it does not remove some characters, such as ;, ', ", and if I use specialChar2, it gives me an error:
java.util.regex.PatternSyntaxException: Syntax error U_REGEX_MISSING_CLOSE_BRACKET near index 32:
How can this be achieved? I searched, but could not find the perfect solution.
source
share