Remove certain characters from a String in Java
This post will discuss how to remove certain characters from a String in Java.
1. Using String.replaceAll() method
The standard solution to replace each substring of a string that matches the given regular expression is using the String.replaceAll() method.
It can be used to remove certain characters from a String, as shown below. The following code replaces all matches of the regular expression \w with an empty string "". Note that \w is a predefined character class that denotes a word character [a-zA-Z_0-9].
|
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { String str = "Hello, World_123!!"; str = str.replaceAll("[^\\w+]", ""); System.out.println(str); } } |
Output:
HelloWorld_123
If you need to retain only a few characters, you can specify those characters after negation ^. For example, [^a-zA-Z] matches with any character except a to z and A to Z.
|
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { String str = "Hello, World_123!!"; str = str.replaceAll("[^a-zA-Z]", ""); System.out.println(str); } } |
Output:
HelloWorld
2. Using String.replace() method
Here’s another solution that uses the String.replace() method to remove all occurrences of each character from the string within a loop. This method might perform relatively slower than the replaceAll() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Main { public static void main(String[] args) { String str = "Hello, World_123!!"; String charsToRemove = ",_!"; for (char c : charsToRemove.toCharArray()) { str = str.replace(String.valueOf(c), ""); } System.out.println(str); } } |
Output:
Hello World123
3. Using Guava
If you prefer the Guava library, you can use its CharMatcher class for removing certain characters from a String. The idea is to get a char matcher that matches with any of the specified characters and remove all matching characters from the specified character set using the removeFrom() method. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import com.google.common.base.CharMatcher; public class Main { public static void main(String[] args) { String str = "Hello, World_123!!"; String charsToRemove = ",_!"; str = CharMatcher.anyOf(charsToRemove).removeFrom(str); System.out.println(str); } } |
Output:
Hello World123
Alternatively, you can use the retainFrom() method to retain all matching characters from the specified character set, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import com.google.common.base.CharMatcher; public class Main { public static void main(String[] args) { String str = "Hello, World_123!!"; String charsToRetain = "0123456789"; str = CharMatcher.anyOf(charsToRetain).retainFrom(str); System.out.println(str); } } |
Output:
123
That’s all about removing certain 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 :)