StringUtils類中isEmpty與isBlank的區(qū)別

依賴:

        <!--lang3工具包,SpringBoot控制依賴版本-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

org.apache.commons.lang3.StringUtils類提供了String的常用操作,最為常用的判空有如下兩種isEmpty(String str)和isBlank(String str)。

用法區(qū)別:

1.StringUtils.isEmpty(String str) 判斷某字符串是否為空,為空的標(biāo)準(zhǔn)是 str==null 或 str.length()==0

System.out.println(StringUtils.isEmpty(null));        //true
System.out.println(StringUtils.isEmpty(""));          //true
System.out.println(StringUtils.isEmpty("   "));       //false
System.out.println(StringUtils.isEmpty("dd"));        //false
//StringUtils.isNotEmpty(String str) 等價于 !isEmpty(String str)

2.StringUtils.isBlank(String str) 判斷某字符串是否為空或長度為0或由空白符(whitespace) 構(gòu)成

System.out.println(StringUtils.isBlank(null));        //true
System.out.println(StringUtils.isBlank(""));          //true
System.out.println(StringUtils.isBlank("   "));       //true
System.out.println(StringUtils.isBlank("dd"));        //false
//StringUtils.isBlank(String str) 等價于 !isBlank(String str)

自己造輪子?

自定義判斷方法,實現(xiàn)同樣的判斷邏輯

    /**
     * 判斷對象是否為null,不允許空白串
     *
     * @param object    目標(biāo)對象類型
     * @return
     */
    public static boolean isNull(Object object){
        if (null == object) {
            return true;
        }
        if ((object instanceof String)){
            return "".equals(((String)object).trim());
        }
        return false;
    }

    /**
     * 判斷對象是否不為null
     *
     * @param object
     * @return
     */
    public static boolean isNotNull(Object object){
        return !isNull(object);
    }

  System.out.println(StringHandler.isNull(null));        //true
  System.out.println(StringHandler.isNull(""));          //true
  System.out.println(StringHandler.isNull("   "));       //true
  System.out.println(StringHandler.isNull("dd"));        //false

常用的String操作

1.字符串編碼轉(zhuǎn)換

 /**
     * change UTF8 To GB2312
     * @param target
     * @return
     */
    public static final String UTF82GB2312(String target) {
        try {
            return new String(target.getBytes("UTF-8"), "gb2312");
        } catch (Exception localException) {
            System.err.println("UTF8 TO GB2312 change error!");
        }
        return null;
    }

    /**
     * change UTF8 To GBK
     * @param target
     * @return
     */
    public static final String UTF82GBK(String target) {
        try {
            return new String(target.getBytes("UTF-8"), "GBK");
        } catch (Exception localException) {
            System.err.println("UTF8 TO GBK change error!");
        }
        return null;
    }

    /**
     * change UTF8 To ISO8859-1
     * @param target
     * @return
     */
    public static final String UTF82ISO(String target) {
        try {
            return new String(target.getBytes("UTF-8"), "ISO8859-1");
        } catch (Exception localException) {
            System.err.println("UTF8 TO ISO8859-1 change error!");
        }
        return null;
    }

    /**
     * change Windows-1252 To UTF-8
     * @param target
     * @return
     */
    public static final String Windows1252UTF8(String target) {
        try {
            return new String(target.getBytes("Windows-1252"), "UTF-8");
        } catch (Exception localException) {
            System.err.println("Windows1252 To UTF8 chage error");
        }
        return null;
    }

2.文本追加高亮

   /**
     * 給串增加顏色標(biāo)簽
     * @param color
     * @param target
     * @return
     */
    public static String withColor(String color, String target) {
        return withColor(color, target,true);
    }
    
    /**
     * 給串增加顏色標(biāo)簽
     * @param color
     * @param target
     * @param paramBoolean
     * @return
     */
    public static String withColor(String color, String target, boolean paramBoolean) {
        if (paramBoolean)
            return "<font color='".concat(color).concat("'>").concat(target).concat("</font>");
        return target;
    }

System.out.println(StringHandler.withColor("red","文本串", true));
<font color='red'>文本串</font>

轉(zhuǎn)載自:https://www.cnblogs.com/dennisit/p/3705374.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容