Home > DeveloperSection > Forums > Remove brackets character in java string
Nigel Bunyan
Nigel Bunyan

Total Post:20

Posted on    December-08-2015 12:39 AM

 Java Java  String 

 1 Reply(s)
 674  View(s)
Rate this:
I want to remove all type of brackets character (example: [],(),{}) in string by using java.

I am doing it like this :

String test = "watching tv (at home)"; 
test = test.replaceAll("(","");
test = test.replaceAll(")","");

But its not working . Suggest me a solution please. 

Mayank Tripathi
Mayank Tripathi

Total Post:397

Posted on    December-08-2015 1:09 AM

To remove all punctuation marks that include all brackets, braces and sq. brackets ... as per the question is:

String test = "watching tv (at home)"; 
test = test.replaceAll("\\p{P}","");
But this solution removes also symbols like ., ,, ;, etc

The first argument of replaceAll takes a regular expression.

All the brackets have meaning in regex: Brackets are used in regex to reference capturing groups, square brackets are used for character class & braces are used for matched character occurrence. Therefore they all need to be escaped...However here the characters can simply be enclosed in a character class with just escaping required for square brackets

test = test.replaceAll("[\\[\\](){}]","");

Don't want to miss updates? Please click the below button!

Follow MindStick