How do I remove some characters from my String

后端 未结 8 914
无人及你
无人及你 2020-12-17 17:03

i want to remove all of the following chars from my string

\">[],-\"

at the moment im doing this. but there must be a more effic

8条回答
  •  天涯浪人
    2020-12-17 17:53

    You can use the replaceAll method of the String class.

    You can form a character class consisting of the characters you want to delete. And the replacement string will be empty string "".

    But the characters you want to delete which you'll be putting in the character class might be regex meta-characters and hence need to be escaped. You can manually escape them as many answers show, alternatively you can use the Pattern.quote() method.

    String charToDel = ">[],-";
    String pat = "[" + Pattern.quote(charToDel) + "]";
    String str = "a>b[c]d,e-f";
    str = str.replaceAll(pat,"");
    

    See it

提交回复
热议问题