package com.wisedu.scc.love.utils; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * String Utils */ public class StringUtil { /** * is null or its length is 0 or it is made by space */ public static boolean isBlank(String str) { return (str == null || str.trim().length() == 0); } /** * is null or its length is 0 */ public static boolean isEmpty(String str) { return (str == null || str.length() == 0); } /** * compare two string */ public static boolean isEquals(String actual, String expected) { return ObjectUtil.isEquals(actual, expected); } /** * null string to empty string */ public static String nullStrToEmpty(String str) { return (str == null ? "" : str); } /** * capitalize first letter */ public static String capitalizeFirstLetter(String str) { if (isEmpty(str)) { return str; } char c = str.charAt(0); return (!Character.isLetter(c) || Character.isUpperCase(c)) ? str : new StringBuilder(str.length()) .append(Character.toUpperCase(c)) .append(str.substring(1)).toString(); } /** * encoded in utf-8 */ public static String utf8Encode(String str) { if (!isEmpty(str) && str.getBytes().length != str.length()) { try { return URLEncoder.encode(str, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException( "UnsupportedEncodingException occurred. ", e); } } return str; } /** * encoded in utf-8, if exception, return defultReturn */ public static String utf8Encode(String str, String defultReturn) { if (!isEmpty(str) && str.getBytes().length != str.length()) { try { return URLEncoder.encode(str, "UTF-8"); } catch (UnsupportedEncodingException e) { return defultReturn; } } return str; } /** * get innerHtml from href */ public static String getHrefInnerHtml(String href) { if (isEmpty(href)) { return ""; } String hrefReg = ".*<[\\s]*a[\\s]*.*>(.+?)<[\\s]*/a[\\s]*>.*"; Pattern hrefPattern = Pattern .compile(hrefReg, Pattern.CASE_INSENSITIVE); Matcher hrefMatcher = hrefPattern.matcher(href); if (hrefMatcher.matches()) { return hrefMatcher.group(1); } return href; } /** * process special char in html */ public static String htmlEscapeCharsToString(String source) { return isEmpty(source) ? source : source .replaceAll("<", "<").replaceAll(">", ">") .replaceAll("&", "&").replaceAll(""", "\""); } /** * transform half width char to full width char */ public static String fullWidthToHalfWidth(String s) { if (isEmpty(s)) { return s; } char[] source = s.toCharArray(); for (int i = 0; i < source.length; i++) { if (source[i] == 12288) { source[i] = ' '; // } else if (source[i] == 12290) { // source[i] = '.'; } else if (source[i] >= 65281 && source[i] <= 65374) { source[i] = (char) (source[i] - 65248); } else { source[i] = source[i]; } } return new String(source); } /** * transform full width char to half width char */ public static String halfWidthToFullWidth(String s) { if (isEmpty(s)) { return s; } char[] source = s.toCharArray(); for (int i = 0; i < source.length; i++) { if (source[i] == ' ') { source[i] = (char) 12288; } else if (source[i] >= 33 && source[i] <= 126) { source[i] = (char) (source[i] + 65248); } else { source[i] = source[i]; } } return new String(source); } /** * 一个散列方法,改变一个字符串(如URL)到一个散列适合使用作为一个磁盘文件名。 */ public static String hashKeyForDisk(String key) { String cacheKey; try { final MessageDigest mDigest = MessageDigest.getInstance("MD5"); mDigest.update(key.getBytes()); cacheKey = bytesToHexString(mDigest.digest()); } catch (NoSuchAlgorithmException e) { cacheKey = String.valueOf(key.hashCode()); } return cacheKey; } /** * count(返回字符串的长度;统计方法,一个汉字或中文字符计2,一个字母或英语标点符号计1) */ public static int count(String str) { int singelC = 0, doubleC = 0; String s = "[^\\x00-\\xff]"; Pattern pattern = Pattern.compile(s); Matcher ma = pattern.matcher(str); while (ma.find()) { doubleC++; } singelC = str.length() - doubleC; if (singelC % 2 != 0) { doubleC += (singelC + 1) / 2; } else { doubleC += singelC / 2; } return doubleC; } /** * 字节转HEX * @param bytes * @return */ private static String bytesToHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(0xFF & bytes[i]); if (hex.length() == 1) { sb.append('0'); } sb.append(hex); } return sb.toString(); } }