使用带花括号“{”的split()时出错

时间:2012-05-23 10:34:54

标签: java android regex split

使用带有花括号

split()时出现问题
 String[] values = str_bj.split("{"); 

我需要使用 { 拆分字符串,但它会在运行时抛出错误.......

错误::

05-23 16:01:30.232: ERROR/AndroidRuntime(6802): FATAL EXCEPTION: main
05-23 16:01:30.232: ERROR/AndroidRuntime(6802): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.obj/com.obj.ObjectDemoActivity}: java.util.regex.PatternSyntaxException: Syntax error U_REGEX_BAD_INTERVAL near index 2:
05-23 16:01:30.232: ERROR/AndroidRuntime(6802): /{
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):   ^
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at android.os.Looper.loop(Looper.java:123)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at java.lang.reflect.Method.invoke(Method.java:521)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at dalvik.system.NativeStart.main(Native Method)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802): Caused by: java.util.regex.PatternSyntaxException: Syntax error U_REGEX_BAD_INTERVAL near index 2:
05-23 16:01:30.232: ERROR/AndroidRuntime(6802): /{
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):   ^
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at com.ibm.icu4jni.regex.NativeRegEx.open(Native Method)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at java.util.regex.Pattern.compileImpl(Pattern.java:383)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at java.util.regex.Pattern.<init>(Pattern.java:341)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at java.util.regex.Pattern.compile(Pattern.java:358)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at java.lang.String.split(String.java:2044)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at com.obj.ObjectDemoActivity.onCreate(ObjectDemoActivity.java:21)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-23 16:01:30.232: ERROR/AndroidRuntime(6802):     ... 11 more

它似乎可以与其他字符一起使用,但不是{

2 个答案:

答案 0 :(得分:20)

{用于表示正则表达式中的间隔,您需要将其转义,因为String.split确实使用了正则表达式。

使用str_bj.split("\\{");str_bj.split("[{]");

答案 1 :(得分:1)

试试这个。样本。

String str = "YourString{YourString{YourString";
String[] temp;
String delimiter = "\\{";
SepString= str.split(delimiter);
  /* print test */
for(int i =0; i < SepString.length ; i++)
System.out.println(SepString[i]);