我想从字符串中删除方括号,但我不知道如何。
String str = "[Chrissman-@1]";
str = replaceAll("\\[\\]", "");
String[] temp = str.split("-@");
System.out.println("Nickname: " + temp[0] + " | Power: " + temp[1]);
但我的结果是:[Chrissman | 1]方括号不会被删除。
我尝试使用不同的正则表达式:"\\[.*?\\]"
,"\\[\\d+\\]"
但结果是一样的,方括号仍然附加在字符串上。
编辑:
我试过:
str.replaceAll("]", "");
str.replaceAll("[", "");
现在我得到:
Exception in thread "Thread-4" java.util.regex.PatternSyntaxException: Unclosed character class near index 0
[
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.clazz(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)