Java拆分字符串不起作用

时间:2018-08-01 12:29:48

标签: java string split

我遇到以下崩溃

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1010, result=-1, data=Intent {  launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 }(has extras) }} to activity {com.forsale.forsale/com.forsale.forsale.view.ItemsActivity}: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 1
},
^
at android.app.ActivityThread.deliverResults(ActivityThread.java:4472)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4515)
at android.app.ActivityThread.-wrap22(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 1
},
^
at java.util.regex.Pattern.compileImpl(Native Method)
at java.util.regex.Pattern.compile(Pattern.java:1340)
at java.util.regex.Pattern.<init>(Pattern.java:1324)
at java.util.regex.Pattern.compile(Pattern.java:946)
at java.lang.String.split(String.java:2325)
at java.lang.String.split(String.java:2367)
at com.forsale.forsale.view.ItemsActivity.preserveAttributes(ItemsActivity.java:1449)
at com.forsale.forsale.view.ItemsActivity.onActivityResult(ItemsActivity.java:1148)
at android.app.Activity.dispatchActivityResult(Activity.java:7256)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4468)

我的代码如下:

String[] items = attributes.split("},");

1 个答案:

答案 0 :(得分:4)

split函数将正则表达式模式作为参数(因为它写在错误中)

}是正则表达式的特殊字符

尝试那样逃脱

   String[] items = attributes.split("\\},");