zoukankan      html  css  js  c++  java
  • 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.

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    H5性能优化
    【JavaScript 】for 循环进化史
    (四十四)通过系统Gallery获取图片
    (四十三)获取图片exif信息
    (四十二)、加载大分辨率图片到内存
    (九)JAVA设计模式之单例模式
    (四十一)Activity切换动画
    (四十)android在代码中,如何设置自定义对话框在屏幕中的位置和大小
    (三十九)android动画 Animation四大属性 详解(转载:http://www.android100.org/html/201304/25/2295.html)
    (三十八)android:windowSoftInputMode属性详解
  • 原文地址:https://www.cnblogs.com/nick-huang/p/4740545.html
Copyright © 2011-2022 走看看