如何从java中的特殊字符中检索字符串

时间:2014-03-26 10:42:06

标签: java java-ee

任何人都可以帮我解决如何从特殊字符中检索州名。我在下面提到了代码

字符串

[["<Select>"],["Andaman and Nicobar"],["Andhra Pradesh"],["Arunachal Pradesh"],["Assam"],["Bihar"],["Chandigarh"],["Chattishgarh"],["CHO Region"],["Dadra & Nagar Haveli"],["Daman & Diu"],["Goa"],["Gujarat"],["Haryana"],["Himachal Pradesh"],["Jammu and Kashmir"],["Jharkhand"],["Karnataka"],["Karnataka Chit"],["Kerala"],["Lakshadweep"],["Madhya Pradesh"],["Maharashtra"],["Maharashtra Chit"],["Manipur"],["Meghalaya"],["Mizoram"],["Nagaland"],["New Delhi"],["NORTH"],["Orissa"],["Pondichery"],["Punjab"],["Rajasthan"],["Sikkim"],["Tamil Nadu"],["Tripura"],["Uttar Pradesh"],["Uttaranchal"],["WEST"],["West Bengal"],null]

预期输出

<Select>,Andaman and Nicobar,Andhra Pradesh,Arunachal Pradesh,Assam,Bihar,Chandigarh,Chattishgarh,CHO Regio,Dadra & Nagar Haveli,Daman & Diu,Goa,Gujarat,Haryana,Himachal Pradesh,Jammu and Kashmir,Jharkhand,Karnataka,Karnataka Chit,Kerala,Lakshadweep,Madhya Pradesh,Maharashtra,Maharashtra Chit,Manipur,Meghalaya,Mizoram,Nagaland,New Delhi,NORTH,Orissa,Pondichery,Punjab,Rajasthan,Sikkim,Tamil Nadu,Tripura,Uttar Pradesh,Uttaranchal,WEST,West Bengal,null

3 个答案:

答案 0 :(得分:1)

您可以使用以下代码

   states = states.replace("[","").replace("]","").replace("\"", "");

答案 1 :(得分:0)

你可以通过删除括号来获得输出。 例如

 String input = "[[\"<Select>\"],[\"Andaman and Nicobar\"],[\"Andhra Pradesh\"],[\"Arunachal Pradesh\"],[\"Assam\"],[\"Bihar\"],[\"Chandigarh\"],[\"Chattishgarh\"],[\"CHO Region\"],[\"Dadra & Nagar Haveli\"],[\"Daman & Diu\"],[\"Goa\"],[\"Gujarat\"],[\"Haryana\"],[\"Himachal Pradesh\"],[\"Jammu and Kashmir\"],[\"Jharkhand\"],[\"Karnataka\"],[\"Karnataka Chit\"],[\"Kerala\"],[\"Lakshadweep\"],[\"Madhya Pradesh\"],[\"Maharashtra\"],[\"Maharashtra Chit\"],[\"Manipur\"],[\"Meghalaya\"],[\"Mizoram\"],[\"Nagaland\"],[\"New Delhi\"],[\"NORTH\"],[\"Orissa\"],[\"Pondichery\"],[\"Punjab\"],[\"Rajasthan\"],[\"Sikkim\"],[\"Tamil Nadu\"],[\"Tripura\"],[\"Uttar Pradesh\"],[\"Uttaranchal\"],[\"WEST\"],[\"West Bengal\"],null]";
    String output1 =  input.replace("[", "");
    String output2 =   output1.replace("]", "");
    System.out.println("output :"+output2);

此代码的输出

output :"<Select>","Andaman and Nicobar","Andhra Pradesh","Arunachal Pradesh","Assam","Bihar","Chandigarh","Chattishgarh","CHO Region","Dadra & Nagar Haveli","Daman & Diu","Goa","Gujarat","Haryana","Himachal Pradesh","Jammu and Kashmir","Jharkhand","Karnataka","Karnataka Chit","Kerala","Lakshadweep","Madhya Pradesh","Maharashtra","Maharashtra Chit","Manipur","Meghalaya","Mizoram","Nagaland","New Delhi","NORTH","Orissa","Pondichery","Punjab","Rajasthan","Sikkim","Tamil Nadu","Tripura","Uttar Pradesh","Uttaranchal","WEST","West Bengal",null

答案 2 :(得分:0)

您可以使用模式:

StringBuilder myFinalString = new StringBuilder();

String myString = "[[\"<Select>\"],[\"Andaman and Nicobar\"],[\"Andhra Pradesh\"],[\"Arunachal Pradesh\"],[\"Assam\"],[\"Bihar\"],[\"Chandigarh\"],[\"Chattishgarh\"],[\"CHO Region\"],[\"Dadra & Nagar Haveli\"],[\"Daman & Diu\"],[\"Goa\"],[\"Gujarat\"],[\"Haryana\"],[\"Himachal Pradesh\"],[\"Jammu and Kashmir\"],[\"Jharkhand\"],[\"Karnataka\"],[\"Karnataka Chit\"],[\"Kerala\"],[\"Lakshadweep\"],[\"Madhya Pradesh\"],[\"Maharashtra\"],[\"Maharashtra Chit\"],[\"Manipur\"],[\"Meghalaya\"],[\"Mizoram\"],[\"Nagaland\"],[\"New Delhi\"],[\"NORTH\"],[\"Orissa\"],[\"Pondichery\"],[\"Punjab\"],[\"Rajasthan\"],[\"Sikkim\"],[\"Tamil Nadu\"],[\"Tripura\"],[\"Uttar Pradesh\"],[\"Uttaranchal\"],[\"WEST\"],[\"West Bengal\"],null]";
Pattern stringPattern = Pattern.compile("\"[^\"]*\"|null");
Matcher matcher = stringPattern.matcher(myString);

while(matcher.find()) {
    myFinalString.append(',').append(matcher.group());
}

System.out.println(myFinalString.toString().substring(1));