String字符串工具類封裝


package com.pay.common.core.utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @類功能說明: String字符串工具類.
 * @類修改者:
 * @修改日期
 * @版本:V1.0
 */
public final class StringUtil {

    private static final Log LOG = LogFactory.getLog(StringUtil.class);

    /**
     * 私有構造方法,將該工具類設爲單例模式.
     */
    private StringUtil() {
    }

    /**
     * 函數功能說明 : 判斷字符串是否爲空 . 修改者名字: 修改日期: 修改內容:
     * 
     * @參數: @param str
     * @參數: @return
     * @return boolean
     * @throws
     */
    public static boolean isEmpty(String str) {
        return null == str || "".equals(str);
    }

    /**
     * 函數功能說明 : 判斷對象數組是否爲空. 修改者名字: 修改日期: 修改內容:
     * 
     * @參數: @param obj
     * @參數: @return
     * @return boolean
     * @throws
     */
    public static boolean isEmpty(Object[] obj) {
        return null == obj || 0 == obj.length;
    }

    /**
     * 函數功能說明 : 判斷對象是否爲空. 修改者名字: 修改日期: 修改內容:
     * 
     * @參數: @param obj
     * @參數: @return
     * @return boolean
     * @throws
     */
    public static boolean isEmpty(Object obj) {
        if (null == obj) {
            return true;
        }
        if (obj instanceof String) {
            return ((String) obj).trim().isEmpty();
        }
        return !(obj instanceof Number) ? false : false;
    }

    /**
     * 函數功能說明 : 判斷集合是否爲空. 修改者名字: 修改日期: 修改內容:
     * 
     * @參數: @param obj
     * @參數: @return
     * @return boolean
     * @throws
     */
    public static boolean isEmpty(List<?> obj) {
        return null == obj || obj.isEmpty();
    }

    /**
     * 函數功能說明 : 判斷Map集合是否爲空. 修改者名字: 修改日期: 修改內容:
     * 
     * @參數: @param obj
     * @參數: @return
     * @return boolean
     * @throws
     */
    public static boolean isEmpty(Map<?, ?> obj) {
        return null == obj || obj.isEmpty();
    }

    /**
     * 函數功能說明 : 獲得文件名的後綴名. 修改者名字: 修改日期: 修改內容:
     * 
     * @參數: @param fileName
     * @參數: @return
     * @return String
     * @throws
     */
    public static String getExt(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    /**
     * 獲取去掉橫線的長度爲32的UUID串.
     * 
     * @author WuShuicheng.
     * @return uuid.
     */
    public static String get32UUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 獲取帶橫線的長度爲36的UUID串.
     * 
     * @author WuShuicheng.
     * @return uuid.
     */
    public static String get36UUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 驗證一個字符串是否完全由純數字組成的字符串,當字符串爲空時也返回false.
     * 
     * @author WuShuicheng .
     * @param str
     *            要判斷的字符串 .
     * @return true or false .
     */
    public static boolean isNumeric(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        } else {
            return str.matches("\\d*");
        }
    }

    /**
     * 計算採用utf-8編碼方式時字符串所佔字節數
     * 
     * @param content
     * @return
     */
    public static int getByteSize(String content) {
        int size = 0;
        if (null != content) {
            try {
                // 漢字採用utf-8編碼時佔3個字節
                size = content.getBytes("utf-8").length;
            } catch (UnsupportedEncodingException e) {
                LOG.error(e);
            }
        }
        return size;
    }

    /**
     * 函數功能說明 : 截取字符串拼接in查詢參數. 修改者名字: 修改日期: 修改內容:
     * 
     * @參數: @param ids
     * @參數: @return
     * @return String
     * @throws
     */
    public static List<String> getInParam(String param) {
        boolean flag = param.contains(",");
        List<String> list = new ArrayList<String>();
        if (flag) {
            list = Arrays.asList(param.split(","));
        } else {
            list.add(param);
        }
        return list;
    }

}

 

發佈了485 篇原創文章 · 獲贊 75 · 訪問量 83萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章