Remove punctuation from a String in Java
This post will discuss how to remove punctuation from a String in Java.
The standard solution to remove punctuations from a String is using the replaceAll() method. It can remove each substring of the string that matches the given regular expression. You can use the POSIX character class \p{Punct} for creating a regular expression that finds punctuation characters.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Main { public static String removePunctuations(String source) { return source.replaceAll("\\p{Punct}", ""); } public static void main(String[] args) { String source = "(A,B)#C:{A_B}[D]"; source = removePunctuations(source); System.out.println(source); // ABCABD } } |
The \p{Punct} class matches with the predefined punctuation character class: p{IsPunctuation}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Main { public static String removePunctuations(String source) { return source.replaceAll("\\p{IsPunctuation}", ""); } public static void main(String[] args) { String source = "(A,B)#C:{A_B}[D]"; source = removePunctuations(source); System.out.println(source); // ABCABD } } |
If you need to remove all whitespaces along with the punctuation characters, you can use the following regex:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Main { public static String removePunctuations(String source) { return source.replaceAll("\\p{Punct}|\\p{Space}", ""); } public static void main(String[] args) { String source = "(A,B)#C: {A_B}[D]"; source = removePunctuations(source); System.out.println(source); // ABCABD } } |
The \p{Punct} class matches with the US-ASCII punctuation by default, i.e., any one of these characters: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~. You can specify any additional characters that you want to remove by adding them in this regex.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Main { public static String removePunctuations(String source) { return source.replaceAll("[!\"#$%&'()*+,-./:;<=>?@\\[\\]^_`{|}~]", ""); } public static void main(String[] args) { String source = "(A,B)#C:::{A_B}[D]"; source = removePunctuations(source); System.out.println(source); // ABCABD } } |
If the regex is called frequently, you might want to compile it to get performance benefits. This can be done by creating a Pattern object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.regex.Pattern; public class Main { private static final Pattern PUNCT_SYMBOLS = Pattern.compile("[!\"#$%&'()*+,-./:;<=>?@\\[\\]^_`{|}~]"); public static String removePunctuations(String source) { return PUNCT_SYMBOLS.matcher(source).replaceAll(""); } public static void main(String[] args) { String source = "(A,B)#C:::{A_B}[D]"; source = removePunctuations(source); System.out.println(source); // ABCABD } } |
That’s all about removing punctuation 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 :)