好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

关于StringUtils.isBlank()的使用及说明

StringUtils.isBlank()的使用

在校验一个String类型的变量是否为空时,可以使用StringUtils.isBlank方法,它可以校验三种情况:是否为null、是否为""、是否为空字符串(引号中间有空格)" "、制表符、换行符、换页符和回车。

StringUtils.isBlank(str); 当str不为空时,返回值为false。  

下面是该方法的源码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

public static boolean isBlank(CharSequence cs) {

        int strLen;

        if (cs != null && (strLen = cs.length()) != 0 ) {

            for ( int i = 0 ; i < strLen; ++i) {

                if (!Character.isWhitespace(cs.charAt(i))) {

                    return false ;

                }

            }

            return true ;

        } else {

            return true ;

        }

    }

在业务代码中使用第三方jar的工具类去判断空,简化开发且方便阅读,类似的还有isEmpty()方法。

?

1

2

3

public static boolean isEmpty(CharSequence cs) {

        return cs == null || cs.length() == 0 ;

    }

通过源码可以看出:

1.isEmpty没有忽略空格参数,是以是否为空和是否存在未判断依据。

2.isBlank进行了为空(字符串为空格、制表符、tab的情况)的判断。一般比较常用。

大家可以通过下面的例子进行体会:

?

1

2

3

4

5

6

StringUtils.isEmpty( "yyy" ) = false

StringUtils.isEmpty( "" ) = true

StringUtils.isEmpty( "   " ) = false

StringUtils.isBlank( "yyy" ) = false

StringUtils.isBlank( "" ) = true

StringUtils.isBlank( "   " ) = true

常用的方法,但是时间久了会记忆模糊,特作出记录,高手可以忽略。。。

StringUtils.isBlank和StringUtils.isEmpty的区别和使用

StringUtils.isBlank和StringUtils.isEmpty都是判断空的方法。

依赖:

?

1

2

3

4

5

< dependency >

             < groupId >org.apache.commons</ groupId >

             < artifactId >commons-lang3</ artifactId >

             < version >3.4</ version >

         </ dependency >

isBlan()的使用

?

1

2

3

4

5

6

7

8

9

boolean b1 = StringUtils.isBlank( null );

         boolean b2 = StringUtils.isBlank( "" );

         boolean b3 = StringUtils.isBlank( " " );

         boolean b4 = StringUtils.isBlank( "\t" );

         boolean b5 = StringUtils.isBlank( "\n" );

         boolean b6 = StringUtils.isBlank( "test" );

         System.out.println( "判断null = " + b1 + '\n' + "判断\"\" = " + b2 + '\n'

                  + "判断空格 = " + b3 + '\n' + "判断制表符 = " + b4 + '\n' + "判断换行符 = " + b5

                 + '\n' + "判断字符串 = " + b6);

isEmpty()的使用

?

1

2

3

4

5

6

7

8

9

boolean b1 = StringUtils.isEmpty( null );

         boolean b2 = StringUtils.isEmpty( "" );

         boolean b3 = StringUtils.isEmpty( " " );

         boolean b4 = StringUtils.isEmpty( "\t" );

         boolean b5 = StringUtils.isBlank( "\n" );

         boolean b6 = StringUtils.isEmpty( "test" );

         System.out.println( "判断null = " + b1 + '\n' + "判断\"\" = " + b2 + '\n'

                 + "判断空格 = " + b3 + '\n' + "判断制表符 = " + b4 + '\n' + "判断换行符 = " + b5

                 + '\n' + "判断字符串 = " + b6);

根据上面结果的对比,isBlank()方法和isEmpty方法的区别:

isBlank() 判断制表符(\t)和空格时,为true; isEmpty() 判断制表符(\t)和空格时,为false;

两个判断null、""、以及换行符(\n或者\r)时为true。

两者的不同主要是因为:

isBlank()源码:

isBlank()判断是否为null和length()为0,还判断是否是空格、tab键、换行符。

isWhitespace() 方法用于判断指定字符是否为空白字符,空白符包含:空格、tab 键、换行符

isEmpty()源码

isEmpty()只判断是否为null和长度为0。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。 

原文链接:https://blog.csdn.net/duan196_118/article/details/110479720

查看更多关于关于StringUtils.isBlank()的使用及说明的详细内容...

  阅读:17次