package com.hcks.cmfds.commons.util; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.math.RoundingMode; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @Author 刘厦(liusha.information@gmail.com) * @Date 创建时间:May 16, 2011 12:25:13 PM * @Version 0.0.0 类说明: 字符串操作工具类 */ public class StringUtils { public static final String SIMPLE_DATE_FORMAT = "yyyy-MM-dd"; public static final String SIMPLE_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; public static final String[] TRUE_STRINGS = new String[] { "true", "yes", "on", "1", "y" }; /** * */ public static final String REGEX_IMG_SRC = "src=\"(.*?)\""; /** * 转换中文参数 * * @param str * @return */ public static String convertEncoding(String str) { if (str == null) return str; try { return new String(str.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return str; } } /** * 获取字符串中所有图片的文件名 * * @param memo * @return */ public static List getImg(String memo) { List list = new ArrayList(); Pattern pa = Pattern.compile(REGEX_IMG_SRC, Pattern.DOTALL); Matcher matcher = pa.matcher(memo); while (matcher.find()) { list.add(matcher.group()); } return list; } private StringUtils() { } /** * 串接字符串(通过 StringBuilder) * * @param strs * @return */ public static String concat(String... strs) { if (strs == null) return null; StringBuilder buff = new StringBuilder(); for (int i = 0; i < strs.length; i++) { buff.append(strs[i]); } return buff.toString(); } /** * 是否为空串 * * @param obj * @return */ public static boolean empty(Object obj) { if (obj == null) return true; String str; if (obj instanceof String) { str = (String) obj; } else { str = obj.toString(); } return str.length() == 0; } /** * 是否不为空串 * * @param obj * @return */ public static boolean notEmp(Object obj) { return !empty(obj); } /** * 转化为int 失败的话返回默认值 * * @param str * @param defaultValue * @return */ public static int toInt(String str, int defaultValue) { try { return Integer.parseInt(str); } catch (NumberFormatException e) { return defaultValue; } } public static int toInt(String str) { return toInt(str, 0); } public static float toFloat(String str) { return toFloat(str, 0); } /** * 转化为float 失败的话返回默认值 * * @param str * @param defaultValue * @return */ public static float toFloat(String str, float defaultValue) { try { return Float.parseFloat(str); } catch (NumberFormatException e) { return defaultValue; } } public static double toDouble(String str) { return toDouble(str, 0); } /** * 转化为double 失败的话返回默认值 * * @param str * @param defaultValue * @return */ public static double toDouble(String str, double defaultValue) { try { return Double.parseDouble(str); } catch (NumberFormatException e) { return defaultValue; } } public static long toLong(String str) { return toLong(str, 0); } /** * 转化为long 失败的话返回默认值 * * @param str * @param defaultValue * @return */ public static long toLong(String str, long defaultValue) { try { return Long.parseLong(str); } catch (NumberFormatException e) { return defaultValue; } } /** * 判断字符串"true" 是否表示 true * * @param str * @return */ public static boolean isTrue(String str) { if (str == null || str.length() == 0) return false; for (int i = 0; i < TRUE_STRINGS.length; i++) { if (TRUE_STRINGS[i].equalsIgnoreCase(str)) { return true; } } return false; } public static boolean isFalse(String str) { return !isTrue(str); } /** * Timestamp转换为日期字符串 * * @param ts * @return */ public static String toDate(Timestamp ts) { if (ts == null) return ""; SimpleDateFormat df = new SimpleDateFormat(SIMPLE_DATETIME_FORMAT); return df.format(ts); } public static Date formatTimestamp(Timestamp ts) { Date d1 = new Date(ts.getTime()); String s1 = formatDate(d1, SIMPLE_DATETIME_FORMAT); return toDate(s1, SIMPLE_DATETIME_FORMAT); } /** * 将字符串解析为Date类型 * * @param date * @param pattern * @return */ public static Date toDate(String date, String pattern) { SimpleDateFormat format = new SimpleDateFormat(pattern); try { return format.parse(date); } catch (ParseException e) { throw new RuntimeException(String.format("Failed to parse the String [%s] to Date.", date), e); } } public static Date toDate(String date) { return toDate(date, SIMPLE_DATETIME_FORMAT); } /** * 格式化日期到字符串 * * @param date * @param pattern * @return */ public static String formatDate(Date date, String pattern) { if (date == null) return ""; SimpleDateFormat format = new SimpleDateFormat(pattern); return format.format(date); } /** * 使用默认格式格式化日期到字符串 * * @param date * @return */ public static String formatDate(Date date) { SimpleDateFormat format = new SimpleDateFormat(SIMPLE_DATE_FORMAT); return format.format(date); } public static double round(double num, int digit) { Double d1 = new Double(num); if(d1.isNaN() || d1.isInfinite()) { return 0.0; } return new BigDecimal(num).setScale(digit, RoundingMode.HALF_UP).doubleValue(); } public static Double round(String num, int digit) { if(isNumeric(num)) { Double d1 = new Double(num); if(d1.isNaN() || d1.isInfinite()) { return 0.0; } return new BigDecimal(num).setScale(digit, RoundingMode.HALF_UP).doubleValue(); }else { return null; } } public static int roundToInt(double num) { return new BigDecimal(num).setScale(0, RoundingMode.HALF_UP).intValue(); } /** * * @方法名称: getUUID * @描述: 获得UUID * @参数 @return * @返回值 String * @抛出异常 */ public static String getUUID() { String s = UUID.randomUUID().toString(); // 去掉“-”符号 return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24); } public static String getFomatString(Double str) { if(StringUtils.notEmp(str)) { java.text.DecimalFormat df =new java.text.DecimalFormat("0.00"); return df.format(str); }else { return null; } } /** * 判断是否为数字字符串 * @param str * @return */ public static boolean isNumeric(String str){ Pattern pattern = Pattern.compile("[0-9]*"); if(notEmp(str)) { Matcher isNum = pattern.matcher(str); if( !isNum.matches() ){ return false; } }else { return false; } return true; } }