代码:
public class Test {
public static void main(String[] args) {
String a = "https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&tn=monline_7_dg&wd=%E5%8F%AA%E6%9C%89%E8%87%AA%E5%B7%B1%E5%BC%BA%E5%A4%A7%E6%89%8D%E8%83%BD%E6%97%A0%E6%89%80%E7%95%8F%E6%83%A7&oq=entry%25E9%259B%2586%25E5%2590%2588&rsv_pq=cee3b365000695f6&rsv_t=feb2fsEkbLc%2F2djov0HPo2JGHCVKdlvXrgd9y%2BbjeNpB8J4Y8XxWO4TBqKWQHVApAm71&rqlang=cn&rsv_enter=1&rsv_dl=ts_3&inputT=14501&rsv_sug3=24&rsv_sug1=15&rsv_sug7=100&rsv_sug2=0&prefixsug=%25E5%258F%25AA%25E6%259C%2589%25E8%2587%25AA%25E5%25B7%25B1%25E5%25BC%25BA%25E5%25A4%25A7%25E6%2589%258D&rsp=3&rsv_sug4=15264";
// 方法1:先取[https://www.baidu.com/s?ie=utf-8,f=3,……],在把索引是0的换成ie=utf-8
/*String[] splits = a.split("\\&");
splits[0]=splits[0].split("\\?")[1];
Map<String,String> map = new HashMap<>();
for (String s :splits){
String[] strings = s.split("=");
map.put(strings[0],strings[1]);
}*/
// 方法2:先取出后面的ie=utf-8&f=3&rsv_bp=1……,在取出[ie=utf-8,f=3,……]
/*int index = a.indexOf("?");
String substring = a.substring(index);
String[] splits = substring.split("\\&");
Map<String,String> map = new HashMap<>();
for (String s :splits){
String[] strings = s.split("=");
map.put(strings[0],strings[1]);
}*/
// 方法3:先取出后面的ie=utf-8&f=3&rsv_bp=1……,在取出[ie,utf-8,f,3,……]
int index = a.indexOf("?");
String substring = a.substring(index);
String[] splits = substring.split("[=\\&]");// 这个地方不同
Map<String,String> map = new HashMap<>();
for (int i=0;i<splits.length;i+=2){
map.put(splits[i],splits[i+1]);
}
// 输出
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String,String> entry :entries){
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
}
}
结果:
oq--->entry%25E9%259B%2586%25E5%2590%2588
rsv_sug7--->100
inputT--->14501
f--->3
rsv_dl--->ts_3
rsv_sug4--->15264
rsv_sug3--->24
rsv_sug2--->0
rsv_sug1--->15
prefixsug--->%25E5%258F%25AA%25E6%259C%2589%25E8%2587%25AA%25E5%25B7%25B1%25E5%25BC%25BA%25E5%25A4%25A7%25E6%2589%258D
rsv_bp--->1
wd--->%E5%8F%AA%E6%9C%89%E8%87%AA%E5%B7%B1%E5%BC%BA%E5%A4%A7%E6%89%8D%E8%83%BD%E6%97%A0%E6%89%80%E7%95%8F%E6%83%A7
rsp--->3
rsv_enter--->1
rsv_t--->feb2fsEkbLc%2F2djov0HPo2JGHCVKdlvXrgd9y%2BbjeNpB8J4Y8XxWO4TBqKWQHVApAm71
tn--->monline_7_dg
?ie--->utf-8
rqlang--->cn
rsv_pq--->cee3b365000695f6
解释:
我感觉使用这种方式也不是特别好,毕竟可能有重复的key,那value是被覆盖的,所以可以让Map集合中的value值是List集合,这样就可以解决这个问题,由于时间原因,这个问题我以后在改正吧!