Last active
October 25, 2017 00:52
-
-
Save liujbo/b2b40103f367b3a35a1f99dc840768fb to your computer and use it in GitHub Desktop.
字符串工具类
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xilai.cuteBoy.common.util; | |
import org.apache.commons.lang.StringUtils; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/************************************************************* | |
* Description: String 工具类 | |
* Author: Liu Junbo | |
* CreateTime: 2017/5/2 | |
************************************************************/ | |
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment