package cn.besteam.common.util; import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.time.DateFormatUtils; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl; import java.text.DecimalFormat; import java.util.Date; import java.util.HashSet; import java.util.Set; import java.util.UUID; /** * @author jacob */ public class StringUtil { public static String trim(String str) { if (str == null) { return ""; } return str.trim(); } /** * 判断A字符串不为NULL并且equals字符串B * * @param valueA * @param valueB * @return * @Date in Apr 14, 2011,5:40:43 PM */ public static boolean strEquals(String valueA, String valueB) { boolean eq = (!isBlank(valueA) && !isBlank(valueB)) && (valueA.trim().equals(valueB.trim())); if (eq) { return true; } return false; } /** *
* 判断是否为空,为空则返回true
* 为空的条件:null、""、"null"
*
*
* @param obj
* @return
*/
public static boolean isBlank(Object obj) {
if (obj == null) {
return true;
}
String str = obj.toString().trim();
if ("".equals(str) || "null".equalsIgnoreCase(str)) {
return true;
}
return false;
}
/**
*
* 判断是否不为空,不为空则返回true
* 为空的条件:null、""、"null"
*
*
* @param obj
* @return
*/
public static boolean isNotBlank(Object obj) {
return !isBlank(obj);
}
/**
*
* 获取换行符
*
*
* @return
*/
public static String getNewLine() {
return System.getProperty("line.separator");
}
/**
* 获取uuid字符串
*/
public static String uuid() {
return UUID.randomUUID().toString();
}
/**
* 获取uuid字符串,无横杠
*/
public static String uuidNotLine() {
return UUID.randomUUID().toString().replace("-", "");
}
/**
* 去除前后空格,若obj为null返回""空字串
*
* @param obj
* @return
*/
public static String trim(Object obj) {
if (obj == null) {
return "";
}
return obj.toString().trim();
}
/**
* * 生成随机码 * *
* 若指定的长度为0,则返回空白字符串 * * @param codeLength 随机码长度 * @return */ public static String randomCode(int codeLength) { return RandomStringUtils.randomNumeric(codeLength); } /** * 取uuid去掉-后从后向前截取指定长度随机串 @Date 2016年3月19日,上午12:57:40 @author YYF @param length @return @return * String @throws */ public static String getUUID(int length) { String uuid = UUID.randomUUID().toString(); uuid = uuid.replace("-", ""); if (uuid.length() > length) { uuid = uuid.substring(uuid.length() - length, uuid.length()); } return uuid; } /** * 判断字符串是否为数字 * * @param str * @return */ public static boolean isNumeric(String str) { if (null == str || str.trim().length() == 0) { return false; } for (int i = 0; i < str.length(); i++) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } /** * 检查指定的字符串是否为空。 *