如何在Java中拆分包含特殊字符的String

时间:2014-11-11 09:26:12

标签: java

我想拆分一个包含值"/ASR/Os_0?type=EcuValues"的字符串,以从字符串中提取"?type=EcuValues"之前的内容。我试过了:

String stringArray[] = stringValue.split("?type=EcuValues") 

但我得到例外。

2 个答案:

答案 0 :(得分:2)

使用字符串拆分

    String s =  "/ASR/Os_0?type=EcuValues" 


    String temp[] =  s.split("?");

//u will get the required chars in temp array

答案 1 :(得分:0)

String#split采用正则表达式,?是具有特殊含义的metacharacter。你应该这样做:

String[] test2 = test.split(Pattern.quote("?type=EcuValues"));
//quote returns string representation of the regex

或者只是转义?

String[] test2 = test.split("\\?type=EcuValues");
相关问题