2012-09-18 15 views
0

我想在两个分号(:)之间分割一个字符串。 即java中两个字符之间的分割

布尔:咖啡先生 - 回忆:8a42bb8b36a6b0820136aa5e05dc01b3:1346790794980

我想

split("[\\:||\\:]"); 

,但它不工作

+0

相关http://stackoverflow.com/questions/4962176/java-extract-part-of-a-string-between-two-special-characters –

回答

3

使用与分裂“: “作为正则表达式。

更确切地说:

String splits[] = yourString.split(":"); 
    //splits will contain: 
    //splits[0] = "BOOLEAN"; 
    //splits[1] = "Mr. Coffee - Recall"; 
    //splits[2] = "8a42bb8b36a6b0820136aa5e05dc01b3"; 
    //splits[3] = "1346790794980"; 
+0

是否有可能分裂第二次出现“:”的字符串,即获得 - BOOLEAN:Mr. Coffee - 回忆 –

0

您可以使用split(),请参阅该代码,

String s ="BOOLEAN: Mr. Coffee - Recall:8a42bb8b36a6b0820136aa5e05dc01b3:1346790794980"; 

String temp = new String(); 


    String[] arr = s.split(":"); 

    for(String x : arr){ 
     System.out.println(x); 
    } 
0

此:

String m = "BOOLEAN: Mr. Coffee - Recall:8a42bb8b36a6b0820136aa5e05dc01b3:1346790794980"; 
    for (String x : m.split(":")) 
    System.out.println(x); 

回报

BOOLEAN 
Mr. Coffee - Recall 
8a42bb8b36a6b0820136aa5e05dc01b3 
1346790794980 
0

正则表达式味:

String yourString = "BOOLEAN: Mr. Coffee - Recall:8a42bb8b36a6b0820136aa5e05dc01b3:1346790794980"; 
    String [] componentStrings = Pattern.compile(":").split(yourString); 

    for(int i=0;i<componentStrings.length;i++) 
    { 
     System.out.println(i + " - " + componentStrings[i]); 
    }