Java String keep only letters and numbers example shows how to keep only letters and numbers in String. The example also shows how to remove all special characters from String in Java.
How to keep only letters and numbers in String?
You can match any character, which is not a letter or a number using the regular expression in Java. You can then remove them using the replaceAll
method of Java String class.
In order to do that, we are going to use the “[^a-zA-Z0-9]” regular expression pattern where,
1 2 3 4 |
^ - means not a-z - means any character between a and z A-Z - means any character between A and Z 0-9 - means any digit between 0-9 |
So our overall pattern “[^a-zA-Z0-9]” means “not any upper or lower case letter and number”. Once we match these characters, we will replace them with the empty string using the replaceAll
method of the String class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javacodeexamples.stringexamples; public class JavaKeepOnlyLetterAndNumbersExample { public static void main(String[] args) { //keep only letters and numbers (alphanumeric) String str = "228, Park Avenue, Building no # 5"; str = str.replaceAll("[^a-zA-Z0-9]", ""); System.out.println(str); } } |
Output
1 |
228ParkAvenueBuildingno5 |
If you want to keep spaces, you can add “\\s” to our pattern to keep them.
1 2 3 4 5 |
//keep only letters, numbers and spaces (alphanumeric) String str = "228, Park Avenue, Building no # 5"; str = str.replaceAll("[^a-zA-Z0-9\\s]", ""); System.out.println(str); |
Output
1 |
228 Park Avenue Building no 5 |
How to keep Unicode characters?
If your string contains Unicode characters, the above given pattern will remove them as well. Consider this example string and its output.
1 2 3 4 5 |
//keep only letters and numbers (alphanumeric) String str = "228, Pärk Ãvenue, Building nö # 5"; str = str.replaceAll("[^a-zA-Z0-9\\s]", ""); System.out.println(str); |
Output
1 |
228 Prk venue Building n 5 |
As you can see from the output, all Unicode characters are removed from the string. If you want to keep Unicode characters, you need to use “[^\\pL\\pN\\s]” pattern instead where,
1 2 3 4 |
^ - means not \\pL - matches any Unicode letter \\pN - matches any Unicode number \\s - matches space character |
Therefore, our pattern means, “match any character which is not Unicode letter, Unicode number or space character”. Here is the final code snippet.
1 2 3 4 |
String str = "228, Pärk Ãvenue, Building nö # 5"; str = str.replaceAll("[^\\pL\\pN\\s]", ""); System.out.println(str); |
Output
1 |
228 Pärk Ãvenue Building nö 5 |
See Also:
This example is a part of the Java String tutorial and Java RegEx tutorial.
Please let me know your views in the comments section below.