天天看點

Apache Commons Lang的StringUtils.isEmpty(STR)和StringUtils.isBlank(STR)

Apache Commons Lang是常用的基礎架構,其中字元串判空在項目中尤為常用,而自己常常忘記他們的差別。

package com.nicchagil.test;

import org.apache.commons.lang3.StringUtils;

public class Call {

    public static void main(String[] args) {
        String NULL_STR = null;
        String EMPTY_STR = "";
        String WHITESPACE_STR = " ";
        String WORD_STR = "A";
        String WORD_WITH_WHITESPACE_STR = " A";

        System.out.println("StringUtils.isEmpty(NULL_STR) -> " + StringUtils.isEmpty(NULL_STR));
        System.out.println("StringUtils.isEmpty(EMPTY_STR) -> " + StringUtils.isEmpty(EMPTY_STR));
        System.out.println("StringUtils.isEmpty(WHITESPACE_STR) -> " + StringUtils.isEmpty(WHITESPACE_STR));
        System.out.println("StringUtils.isEmpty(WORD_STR) -> " + StringUtils.isEmpty(WORD_STR));
        System.out.println("StringUtils.isEmpty(WORD_WITH_WHITESPACE_STR) -> " + StringUtils.isEmpty(WORD_WITH_WHITESPACE_STR));

        System.out.println();

        System.out.println("StringUtils.isBlank(NULL_STR) -> " + StringUtils.isBlank(NULL_STR));
        System.out.println("StringUtils.isBlank(EMPTY_STR) -> " + StringUtils.isBlank(EMPTY_STR));
        System.out.println("StringUtils.isBlank(WHITESPACE_STR) -> " + StringUtils.isBlank(WHITESPACE_STR));
        System.out.println("StringUtils.isBlank(WORD_STR) -> " + StringUtils.isBlank(WORD_STR));
        System.out.println("StringUtils.isBlank(WORD_WITH_WHITESPACE_STR) -> " + StringUtils.isBlank(WORD_WITH_WHITESPACE_STR));
    }

}           

日志:

StringUtils.isEmpty(NULL_STR) -> true
StringUtils.isEmpty(EMPTY_STR) -> true
StringUtils.isEmpty(WHITESPACE_STR) -> false
StringUtils.isEmpty(WORD_STR) -> false
StringUtils.isEmpty(WORD_WITH_WHITESPACE_STR) -> false

StringUtils.isBlank(NULL_STR) -> true
StringUtils.isBlank(EMPTY_STR) -> true
StringUtils.isBlank(WHITESPACE_STR) -> true
StringUtils.isBlank(WORD_STR) -> false
StringUtils.isBlank(WORD_WITH_WHITESPACE_STR) -> false           

摘抄API說明:

org.apache.commons.lang3.StringUtils.isEmpty(CharSequence cs) : Checks if a CharSequence is empty (“”) or null.

org.apache.commons.lang3.StringUtils.isBlank(CharSequence cs) : Checks if a CharSequence is whitespace, empty (“”) or null.

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

作者:Nick Huang 部落格:http://www.cnblogs.com/nick-huang/

本部落格為學習、筆記之用,以筆記形式記錄學習的知識與感悟。學習過程中可能參考各種資料,如覺文中表述過分引用,請務必告知,以便迅速處理。如有錯漏,不吝賜教。

如果本文對您有用,

點贊

評論

哦;如果您喜歡我的文章,請點選

關注我

哦~