Java replace all square brackets in a string

前端 未结 7 1200
执笔经年
执笔经年 2020-12-02 11:20

I want to remove square brackets from a string, but I don\'t know how.

String str = \"[Chrissman-@1]\";
str = replaceAll(\"\\\\[\\\\]\", \"\");

String[] tem         


        
7条回答
  •  孤街浪徒
    2020-12-02 11:52

    Your regex matches (and removes) only subsequent square brackets. Use this instead:

    str = str.replaceAll("\\[|\\]", "");
    

    If you only want to replace bracket pairs with content in between, you could use this:

    str = str.replaceAll("\\[(.*?)\\]", "$1");
    

提交回复
热议问题