1 Star 3 Fork 3

梁-Gavin/common

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
StringUtil.java 3.82 KB
一键复制 编辑 原始数据 按行查看 历史
梁-Gavin 提交于 2017-09-22 10:27 . 初始提交
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;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/lgqBlog/common.git
[email protected]:lgqBlog/common.git
lgqBlog
common
common
master

搜索帮助