java中获取字符串中信息
请教各位:我现在想在有条字符串
如:
“aaa=44:55:22:55 bb=123.123.123.123 tt=4658655675685,cc=ACP-400,Ver=AC Ver 4.1.9 Apr 23 2012
aaa=44:55:22:55 bb=123.123.123.123 tt=4658655675685,cc=ACP-400,Ver=AC Ver 4.1.9 Apr 23 2012
” 这种格式的字符串,有没有办法将里面属性单独提取出来,里面属性值是可变的,属性名称不变;
java中获取字符串中信息
------解决方案--------------------只提供思路。
String str ="aaa=44:55:22:55 bb=123.123.123.123 tt=4658655675685,cc=ACP-400,Ver=AC Ver 4.1.9 Apr 23 2012"+
"aaa=44:55:22:55 bb=123.123.123.123 tt=4658655675685,cc=ACP-400,Ver=AC Ver 4.1.9 Apr 23 2012";
String[] strs = str.split("\\w*=");
for(String s : strs){
System.out.println(s);
}
------解决方案--------------------
修改一下
public static void main(String[] args) {
String str = "aaa=44:55:22:55 bb=123.123.123.123 tt=4658655675685,cc=ACP-400,Ver=AC Ver 4.1.9 Apr 23 2012";
Map map = zfc(str);
System.out.println(map.toString());
System.out.println(map.get("aaa"));
}
public static Map zfc(String str){
String[] one = str.split(",");
Map map = new HashMap();
for(int i = 0; i<one.length; i++){
if(i == 0){
String[] two = one[0].split(" ");
for(int j = 0; j < two.length; j++){
String key = two[j].substring(0, two[j].indexOf("="));
String value = two[j].substring(two[j].indexOf("=")+1, two[j].length());
map.put(key, value);
}
}else{
String key = one[i].substring(0, one[i].indexOf("="));
String value = one[i].substring(one[i].indexOf("=")+1, one[i].length());
map.put(key, value);
}
}
return map;
}