代码拉取完成,页面将自动刷新
package com.zheng.common.util;
import org.apache.commons.lang.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* String 工具类
* Created by shuzheng on 2016/12/07.
*/
public class StringUtil {
private static Pattern linePattern = Pattern.compile("_(\\w)");
private static Pattern humpPattern = Pattern.compile("[A-Z]");
/**
* 下划线转驼峰
* @param str
* @return
*/
public static String lineToHump(String str) {
if (null == str || "".equals(str)) {
return str;
}
str = str.toLowerCase();
Matcher matcher = linePattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);
str = sb.toString();
str = str.substring(0, 1).toUpperCase() + str.substring(1);
return str;
}
/**
* 驼峰转下划线(简单写法,效率低于{@link #humpToLine2(String)})
* @param str
* @return
*/
public static String humpToLine(String str) {
return str.replaceAll("[A-Z]", "_$0").toLowerCase();
}
/**
* 驼峰转下划线,效率比上面高
* @param str
* @return
*/
public static String humpToLine2(String str) {
Matcher matcher = humpPattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* 首字母转小写
* @param s
* @return
*/
public static String toLowerCaseFirstOne(String s) {
if (StringUtils.isBlank(s)) {
return s;
}
if (Character.isLowerCase(s.charAt(0))) {
return s;
} else {
return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
}
}
/**
* 首字母转大写
* @param s
* @return
*/
public static String toUpperCaseFirstOne(String s) {
if (StringUtils.isBlank(s)) {
return s;
}
if (Character.isUpperCase(s.charAt(0))) {
return s;
} else {
return (new StringBuffer()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
}
}
/**
* object转String
* @param object
* @return
*/
public static String getString(Object object) {
return getString(object, "");
}
public static String getString(Object object, String defaultValue) {
if (null == object) {
return defaultValue;
}
try {
return object.toString();
} catch (Exception e) {
return defaultValue;
}
}
/**
* object转Integer
* @param object
* @return
*/
public static int getInt(Object object) {
return getInt(object, -1);
}
public static int getInt(Object object, Integer defaultValue) {
if (null == object) {
return defaultValue;
}
try {
return Integer.parseInt(object.toString());
} catch (Exception e) {
return defaultValue;
}
}
/**
* object转Boolean
* @param object
* @return
*/
public static boolean getBoolean(Object object) {
return getBoolean(object, false);
}
public static boolean getBoolean(Object object, Boolean defaultValue) {
if (null == object) {
return defaultValue;
}
try {
return Boolean.parseBoolean(object.toString());
} catch (Exception e) {
return defaultValue;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。