更新時間:2022-12-08 11:37:51 來源:動力節點 瀏覽1348次
1.指定格式參數轉map
/**
* 將name1=value1;name2=value2形式的字符串轉化為Map<name, value>
* @param values 字符串對象
* @param keySeparator 多個鍵值對之間分隔符
* @param valueSeparator 各鍵值之間分隔符
* @return
*/
public static Map<String, String> str2Map(String values, String keySeparator, String valueSeparator) {
Map<String, String> map = new HashMap<>();
values = values == null ? "" : values;
String[] nameValues = values.split(keySeparator);
for (int i = 0; i < nameValues.length; i++) {
String[] tmp = nameValues[i].split(valueSeparator);
// 有的字段值可能為空
if (tmp.length == 2) {
map.put(tmp[0].trim(), tmp[1]);
} else {
map.put(tmp[0].trim(), "");
}
}
return map;
}
2. 字符串null則返回默認值
/**
* @Deprecated 字符串為空則賦值默認值
* @param str 字符串對象
* @param defaultValue 默認值
* @return
*/
public static String nullOrDefault(String str, String defaultValue) {
return str == null ? defaultValue : str.trim();
}
3.駝峰轉下劃線
private static Pattern humpPattern = Pattern.compile("[A-Z]");
private static final String UNDERLINE = "_";
/**
* 駝峰轉下劃線
* @param str
* @return
*/
public static String camel2Underline(String str) {
if (str == null || str.equals("")) {
return "";
}
Matcher matcher = humpPattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, UNDERLINE + matcher.group(0).toLowerCase());
}
matcher.appendTail(sb);
return sb.toString();
}
4.下劃線轉駝峰
private static Pattern linePattern = Pattern.compile("_(\\w)");
/**
* 下劃線轉駝峰
* @param str
* @return
*/
public static String underline2Camel(String str) {
if (str == null || str.equals("")) {
return "";
}
Matcher matcher = linePattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);
return sb.toString();
}
5. 移除右側的零
/**
* 移除右側的零
* @param str
* @return
*/
public static String removeZeroRight(String str) {
return str.replaceAll("0*$","");
}
6. 移除左側的零
/**
* 移除左側的零
* @param str
* @return
*/
public static String removeZeroLeft(String str) {
return str.replaceAll("^0*","");
}
7. 右側補零
/**
* 右側補零
* @param str
* @param n
* @return
*/
public static String addZeroRight(String str, int n) {
if (str.length() >= n) {
return str;
}
return str + String.format("%1$0" + (n - str.length()) + "d", 0);
}
8. 左側補零
/**
* 左側補零
* @param str
* @param n
* @return
*/
public static String addZeroLeft(String str, int n) {
if (str.length() >= n) {
return str;
}
return String.format("%1$0" + (n - str.length()) + "d", 0) + str;
}
9. 根據證件號獲取生日信息
/**
* 根據身份證獲取生日信息
* @param idCard 18位身份證號
* @return year-MM-dd
*/
public static String getBirthByIdCard(String idCard) {
if (isEmpty(idCard)){
return "";
}
String birth = idCard.substring(6, 10) + MIDDLELINE + idCard.substring(10, 12) + MIDDLELINE + idCard.substring(12,14);
return birth;
}
10. 根據證件號判斷是否是男性
/**
* 判斷是否是男性
* @param idCard 18位身份證號
* @return
*/
public static Boolean isManByIdCard(String idCard){
if (isEmpty(idCard)){
return null;
}
String str = String.valueOf(idCard.charAt(idCard.length() - 2));
int sexNum = Integer.valueOf(str);
return sexNum % 2 == 1;
}
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習