English 中文(简体)
使用卷曲括号“ {” 的拆分 () 错误
原标题:Error using split() with curly braces "{"

存在使用 spplit () 和 < 强/ 强 > 牙套的问题

 String[] values = str_bj.split("{"); 

需要使用“ 强势”\\ code\ \ / code\ \ \ \ / 强” 来拆分字符串, 但它在运行时丢出一个错误......

错误 :

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

它似乎对其他字符有效, 但不能使用

最佳回答

用于代表正则表达式中的间隔, 您需要避开它, 因为 string.split 确实使用正则表达式 。

使用 str_bj.split (""); str_bj.split ("{}");

问题回答

试试这个 标本

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]);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...