zoukankan      html  css  js  c++  java
  • Java中StringHelp

    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("-", "");
        }
        
    }
  • 相关阅读:
    Swift 编程语言新手教程
    标准差(standard deviation)和标准错误(standard error)你能解释一下?
    shell文字过滤程序(十一):paste命令
    java 获取系统变量(环境变量和环境变量)
    MD5算法原理
    受托停止事件冒泡
    搜索引擎优化要领:8条辅助技巧(三)
    几种更新(Update语句)查询的方法
    学习盲点
    2014年同年CFA考试中哪些CFA资料没有变化?
  • 原文地址:https://www.cnblogs.com/shangshen/p/10972374.html
Copyright © 2011-2022 走看看