在java中替换“[”String时发生异常

时间:2013-09-19 08:03:02

标签: java android

我想从字符串中删除“[”和“]”字符。

我的代码是:

String original=data.replaceAll("]|[", "");

我收到错误:

09-19 13:25:55.755: E/AndroidRuntime(25007): FATAL EXCEPTION: main
09-19 13:25:55.755: E/AndroidRuntime(25007): java.util.regex.PatternSyntaxException: Syntax error U_REGEX_MISSING_CLOSE_BRACKET near index 3:
09-19 13:25:55.755: E/AndroidRuntime(25007): ]|[
09-19 13:25:55.755: E/AndroidRuntime(25007):    ^
09-19 13:25:55.755: E/AndroidRuntime(25007):    at com.ibm.icu4jni.regex.NativeRegEx.open(Native Method)
09-19 13:25:55.755: E/AndroidRuntime(25007):    at java.util.regex.Pattern.compileImpl(Pattern.java:383)
09-19 13:25:55.755: E/AndroidRuntime(25007):    at java.util.regex.Pattern.<init>(Pattern.java:341)
09-19 13:25:55.755: E/AndroidRuntime(25007):    at java.util.regex.Pattern.compile(Pattern.java:358)
09-19 13:25:55.755: E/AndroidRuntime(25007):    at java.lang.String.replaceAll(String.java:2004)

1 个答案:

答案 0 :(得分:5)

改为使用

String original=data.replaceAll("\\]|\\[", "");