Remove all non-alphanumeric characters from a String in Java
This post will discuss how to remove all non-alphanumeric characters from a String in Java.
1. Using String.replaceAll() method
A common solution to remove all non-alphanumeric characters from a String is with regular expressions. The idea is to use the regular expression [^A-Za-z0-9] to retain only alphanumeric characters in the string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Main { public static String removeAllNonAlphaNumeric(String s) { if (s == null) { return null; } return s.replaceAll("[^A-Za-z0-9]", ""); } public static void main(String[] args) { String s = "(A)B,C|D_E1"; System.out.println(removeAllNonAlphaNumeric(s)); } } |
Output:
ABCDE1
You can also use [^\w] regular expression, which is equivalent to [^a-zA-Z_0-9]. It will replace characters that are not present in the character range A-Z, a-z, 0-9, _. Alternatively, you can use the character class \W that directly matches with any non-word character, i.e., [a-zA-Z_0-9].
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Main { public static String removeAllNonAlphaNumeric(String s) { if (s == null) { return null; } return s.replaceAll("[\\W]+", ""); } public static void main(String[] args) { String s = "(A)B,C|D_E1"; System.out.println(removeAllNonAlphaNumeric(s)); } } |
Output:
ABCD_E1
Note, this solution retains the underscore character. If you need to remove underscore as well, you can use regex [\W]|_. Alternatively, you can use the POSIX character class \p{Alnum}, which matches with any alphanumeric character [A-Za-z0-9]. It is equivalent to [\p{Alpha}\p{Digit}].
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Main { public static String removeAllNonAlphaNumeric(String s) { if (s == null) { return null; } return s.replaceAll("[^\\p{Alnum}]", ""); } public static void main(String[] args) { String s = "(A)B,C|D_E1"; System.out.println(removeAllNonAlphaNumeric(s)); } } |
Output:
ABCDE1
2. Using Guava
If you use the Guava library in your project, you can use its javaLetterOrDigit() method from CharMatcher class to determine whether a character is an alphabet or a digit. You can remove or retain all matching characters returned by javaLetterOrDigit() method using the removeFrom() and retainFrom() method respectively. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import com.google.common.base.CharMatcher; public class Main { public static String removeAllNonAlphaNumeric(String s) { if (s == null) { return null; } return CharMatcher.javaLetterOrDigit().retainFrom(s); } public static void main(String[] args) { String s = "(A)B,C|D_E1"; System.out.println(removeAllNonAlphaNumeric(s)); } } |
Output:
ABCDE1
You can also specify the range of characters to be removed or retained in a String using the static method inRange() of the CharMatcher class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import com.google.common.base.CharMatcher; public class Main { public static String removeAllNonAlphaNumeric(String s) { if (s == null) { return null; } return CharMatcher.inRange('a', 'z') .or(CharMatcher.inRange('A', 'Z') .or(CharMatcher.inRange('0', '9'))) .retainFrom(s); } public static void main(String[] args) { String s = "(A)B,C|D_E1"; System.out.println(removeAllNonAlphaNumeric(s)); } } |
Output:
ABCDE1
That’s all about removing all non-alphanumeric characters from a String in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)