Filter a String in Java
This post will discuss how to filter a string in Java. The solution should return a subsequence of the input string that omits all matching characters of a character sequence.
1. Using String.replaceAll() method
The most commonly used solution to remove desired characters from the input string is to use the replaceAll() method. It replaces each substring of the string that matches the given regular expression with the specified replacement.
For example, consider the following code, which removes all digits from the given string by passing regex [\d] to replaceAll() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
class Main { public static String removeNumeric(String str) { return str.replaceAll("[\\d]", ""); } public static void main(String[] args) { String input = "A1B2C3D4E5"; System.out.println(removeNumeric(input)); // prints `ABCDE` } } |
Note that an empty string is used as a substitution for each match. The regular expression [0-9] can also match for numeric characters.
To return the string containing only digits, change the regular expression to [^\d] or [^0-9] that replaces every non-numeric character sequence with an empty sequence. Similarly, [^\W] can be used to remove all alphanumeric characters (along with underscore) from the string, while [\W] can be used to permit alphanumeric characters and an underscore.
2. Using Java 8
In Java 8 and above, use chars() or codePoints() method of String class to get an IntStream of char values from the given sequence. Then call the filter() method of Stream for restricting the char values to match the given predicate. Finally, collect the values in the resultant stream with StringBuilder to construct a String.
Consider the following code which filters numeric values from the given string using Stream:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.stream.Collectors; class Main { public static String removeNumeric(String str) { return str.chars() // or `codePoints()` .filter(ch -> !Character.isDigit(ch)) .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) .toString(); } public static void main(String[] args) { String input = "A1B2C3D4E5"; System.out.println(removeNumeric(input)); // prints `ABCDE` } } |
3. Using CharMatcher by Guava
Several third-party libraries provide utility methods for working with strings. If you prefer Google’s Guava library, use its CharMatcher class that offers several utility methods for manipulating strings.
Following methods by CharMatcher class can be used together to remove numeric values from the given string:
⮚ removeFrom() with inRange():
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.base.CharMatcher; class Main { public static String removeNumeric(String str) { return CharMatcher.inRange('0', '9') .removeFrom(str); } public static void main(String[] args) { String input = "A1B2C3D4E5"; System.out.println(removeNumeric(input)); // prints `ABCDE` } } |
⮚ removeFrom() with anyOf():
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.base.CharMatcher; class Main { public static String removeNumeric(String str) { return CharMatcher.anyOf("0123456789") .removeFrom(str); } public static void main(String[] args) { String input = "A1B2C3D4E5"; System.out.println(removeNumeric(input)); // prints `ABCDE` } } |
⮚ removeFrom() with digit():
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.base.CharMatcher; class Main { public static String removeNumeric(String str) { return CharMatcher.digit() .removeFrom(str); } public static void main(String[] args) { String input = "A1B2C3D4E5"; System.out.println(removeNumeric(input)); // prints `ABCDE` } } |
⮚ retainFrom() with noneOf():
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.base.CharMatcher; class Main { public static String removeNumeric(String str) { return CharMatcher.noneOf("0123456789") .retainFrom(str); } public static void main(String[] args) { String input = "A1B2C3D4E5"; System.out.println(removeNumeric(input)); // prints `ABCDE` } } |
That’s all about filtering a Java String.
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 :)