Java中StringHelp

本文介绍了一个Java中用于处理字符串的工具类,包括判断字符串是否为空、转换NULL为字符串、模糊查询字符串转换以及获取GUID等功能。适用于各种需要对字符串进行高效处理的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值