從 “key1=value1&key2=value2…” 的字串中,根據key獲取value的工具類
阿新 • • 發佈:2021-01-31
從 “key1=value1&key2=value2…” 的字串中,根據key獲取value的工具類:
package com.mars.cloud.user.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Project: <br>
* @CreateDate: 2021/01/30 11:42 <br>
* @Author: <br>
* @Description: 字串工具類
*/
public class StringUtil {
/**
* 根據key,獲取key1=value1&key2=value2....中的value
*
* @param strContent 目標字串
* @param param 要獲取的鍵
* @return 要獲取的值
*/
public static String getParam(String strContent, String param) {
Pattern p = Pattern.compile("(^|&)" + param + "=([^&]*)(&|$)");
Matcher m = p.matcher(strContent);
String matcherStr = "";
while (m.find()) {
matcherStr = m.group(2);
}
return matcherStr;
}
}