Brian McCanaugh
Brian McCanaugh

Reputation: 165

remove bracket, double quote, and possibly add space using string.replaceall in java

I saw multiple thread answering about how to replace/remove single quote ('), double quote ("), bracket ([,{), commas, etc. While I was able to successfully remove them, but I would like to understand more. For example, string.replaceAll("\p{P}",""); can remove the punctuations. I am confused about this syntax; how does "\p{P}","" is equal to punctuations?

I have a string that I would like to remove bracket, double quote, and possibly add space. As shown below, I would like to use replaceAll to change my string from category to updatedCategory.

String category = "["restaurant","bar","burger joint"]";
String updatedCategory = "restaurant, bar, burger joint";

Upvotes: 0

Views: 986

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60026

You need to learn about regex, for your problem you can use replaceAll with this regex ["\[ \]] like this:

category.replaceAll("[\"\\[ \\]]", "")

The output will be:

restaurant,bar,burgerjoint

So to get the same updatedCategory just use:

category.replaceAll("[\"\\[ \\]]", "")
    .replace(",", ", ")

Upvotes: 2

Related Questions