import java.util.Collection; import java.util.Map; import java.util.UUID; public class StringHelper { public static final String EMPTY=""; public StringHelper(){} /** * 判断字符串是否为NULL或空值 * @param str * @return */ @SuppressWarnings("rawtypes") public static Boolean IsEmptyOrNull(Object obj) { if (obj == null) return true; if ("".equals(obj)) return true; if (obj instanceof String) { if (((String) obj).length() == 0) { return true; } } else if (obj instanceof Collection) { if (((Collection) obj).size() == 0) { return true; } } else if (obj instanceof Map) { if (((Map) obj).size() == 0) { return true; } } return false; } /** * NULL转String * @param obj * @return */ public static String TranString(Object obj) { if (obj == null) return ""; return obj.toString(); } /** * NULL转String * @param obj * @param defaultval 默认值 * @return */ public static String TranString(Object obj, String defaultval) { if (obj == null) return defaultval; return obj.toString(); } /** * 模糊查询%_通配符转换 * @param str * @return */ public static String TranSeachString(Object obj) { if (obj == null) return ""; String result = obj.toString(); return result.replace("\\", "\\\\").replace("_", "\\_").replace("%", "\\%"); /*if (result.startsWith("%") || result.startsWith("_")) { result = "\\" + result; } if (!"\\%".equals(result) && !"\\_".equals(result) && result.length() >= 2 && (result.endsWith("%") || result.endsWith("_"))) { String tmp1 = result.substring(result.length() - 1, result.length()); String tmp2 = result.substring(0, result.length() - 1); result = tmp2 + "\\" + tmp1; } return result;*/ } /** * 获取GUID * @return */ public static String GetGUID() { return UUID.randomUUID().toString().replace("-", ""); } }
原文:https://www.cnblogs.com/shangshen/p/10972374.html