您的位置 首页 java

java字符串常用的方法

1. 字符串null则返回默认值

/**

* @Deprecated 字符串为空则赋值默认值

* @param str 字符串对象

* @param defaultValue 默认值

* @return

*/

public static String nullOrDefault(String str, String defaultValue) {

return str == null ? defaultValue : str.trim();

}

2. 指定格式参数转 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;

}

3. 下划线转驼峰

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();

}

4. 驼峰转下划线

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();

}

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;

}

以上是字符串业务数据层面操作的一些封装的方法,后续有机会还会补充~

文章来源:智云一二三科技

文章标题:java字符串常用的方法

文章地址:https://www.zhihuclub.com/188060.shtml

关于作者: 智云科技

热门文章

网站地图