Remove Punctuation From String in Java
Mohammad Irfan
Dec 10, 2020
Sep 19, 2020

This tutorial introduces how to remove punctuation from a string in Java.
Punctuation is basically some special chars that are used to make a text grammatically correct. Some punctuation marks are comma(,), colon(:), question marks(?), etc. Let’s see some examples in Java.
Remove Punctuation From String Using the replaceAll()
Method in Java
We can use a regex pattern in the replaceAll()
method with the pattern as \\p{Punct}
to remove all the punctuation from the string and get a string punctuation free. The regex pattern is \\p{Punct}
, which means all the punctuation symbols. See the example below.
public class SimpleTesting {
public static void main(String[] args){
String str = "String - is a squence of chars:~!@#$%^&*(). Test.";
System.out.println(str);
String result = str.replaceAll("\\p{Punct}", "");
System.out.println(result);
}
}
Output:
String - is a squence of chars:~!@#$%^&*(). Test.
String is a squence of chars Test
Related Article - Java String
- Perform String to String Array Conversion in Java
- Remove Substring From String in Java
- Convert Byte Array in Hex String in Java
- Convert Java String Into Byte
- Generate Random String in Java
- The Swap Method in Java