package com.persagy.cameractl.utils; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; public final class StringTools { /** * 获取UUID字符串 * * @param length 指定字符长度,范围(0,36],若为null则视length=36处理 * @return 固定长度的数字+小写字母+中划线组合的UUID字符串 */ public static String getUUID(Integer length) { String uuidStr = UUID.randomUUID().toString(); if (null != length) { length = length > 36 ? 36 : length; uuidStr = uuidStr.substring(0, length); } return uuidStr; } /** * 获取32位长度UUID * @return 32长度,只包含数字+小写字母 */ public static String getUUID() { return getUUID(36).replace("-", ""); } public static void main(String[] args) { System.out.println(getUUID()); } /** * 获取字符串str对应的hash散列结果值 * @param str * @return 000-999之间的数字字符串 */ public static String getHashNum(String str) { str = str == null ? "" : str; // 得到哈希正整数 int hashInt = Math.abs(str.hashCode()); // 设置哈希字符值,长度为4位 hashInt = 100000 + hashInt % 1000; String hashStr = ("" + hashInt).substring(3); return hashStr; } /** * 提供空字符串. */ public static final java.lang.String EMPTY = ""; /** * 判断字符串是否为null,或者是空串"", 或者是空格串" " * * @param str * @return boolean */ public static boolean isBlank(String str) { if (isEmpty(str)) { return true; } return str.trim().length() == 0; } /** * 如果源字符串str为null,空串或空格串则返回默认字符串 * * @param str 源字符串 * @param defStr 默认字符串 * @return String */ public static String getDefault(String str, String defStr) { return isBlank(str) ? defStr : str; } /** * 判断字符串是否为null,或者是空串"" . * * @param s * @return boolean */ public static boolean isEmpty(String s) { return (s == null || s.length() == 0); } /** * 判断字符串是否不为null,且不是空串"" . * * @param s * @return boolean */ public static boolean isNotEmpty(String s) { return !isEmpty(s); } public static final String toString(Object o) { return o != null ? o.toString() : null; } /** * 判断是否为空格字符串 . * * @param cs * @return boolean */ public static boolean isBlank(CharSequence cs) { if (null == cs) return false; int length = cs.length(); for (int i = 0; i < length; i++) { if (!Character.isWhitespace(cs.charAt(i))) return false; } return true; } /** * 比较字符串是否相等,都为null时,返回true . * * @param string1 * @param string2 * @return boolean */ public static boolean equalsWithNull(String string1, String string2) { if (string1 == null) { if (string2 == null) { return true; } else { return false; } } else { return string1.equals(string2); } } public static boolean checkIsPhone(String phonenumber) { String phone = "(^(d{3,4}-)?d{7,8})$|(1[0-9]{10})"; String tregEx = "[0-9]{7,8}"; // 表示a或f 0832-80691990 Pattern p = Pattern.compile(phone); Matcher m = p.matcher(phonenumber); Pattern p2 = Pattern.compile(tregEx); Matcher m2 = p2.matcher(phonenumber); // String regEx="[1]{1}[3,5,8,6]{1}[0-9]{9}"; //手机 // String tregEx="[0]{1}[0-9]{2,3}-[0-9]{7,8}"; //表示a或f 0832-80691990 return m.matches() || m2.matches(); } /** * 判断字符串中是否包含汉字,如字符串为空,返回false . * * @param str * @return boolean */ public static boolean hasHanZi(String str) { if (StringTools.isEmpty(str)) { return false; } String regEx = "[\\u4e00-\\u9fa5]"; Pattern pat = Pattern.compile(regEx); Matcher mat = pat.matcher(str); return mat.find(); } /** * 去除非汉字内容 */ public static String clearNotChinese(String buff){ String tmpString =buff.replaceAll("(?i)[^a-zA-Z0-9\u4E00-\u9FA5]", "");//去掉所有中英文符号 char[] carr = tmpString.toCharArray(); for(int i = 0; i 255) { return false; } else if (isBlank(fileName)) { return true; } else { return fileName.matches( "[^\\s\\\\/:\\*\\?\\\"<>\\|](\\x20|[^\\s\\\\/:\\*\\?\\\"<>\\|])*[^\\s\\\\/:\\*\\?\\\"<>\\|\\.]$"); } } /** * 将字符串首字母大写 . * * @param s * @return String */ public static String capitalize(CharSequence s) { if (null == s) return null; int len = s.length(); if (len == 0) return ""; char char0 = s.charAt(0); if (Character.isUpperCase(char0)) return s.toString(); return new StringBuilder(len).append(Character.toUpperCase(char0)).append(s.subSequence(1, len)).toString(); } /** * 判断String是否为null或空 * * @param params * @return */ public static boolean isNull(String... params) { for (String param : params) { if (param == null || "".equals(param)) { return true; } } return false; } }