`

Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)

阅读更多

Bash字符串处理(与Java对照) - 6.判断字符串是否为空(不为空)

In Java

判断是否为null

是null

if (s == null) {

    // do something

}

 

不是null

if (s != null) {

    // do something

}

 

判断是否null或空串

是null或者空串:方法一

if (s == null || s.length() == 0) {

    // do something

}

 

是null或者空串:方法二

if (s == null || s.isEmpty()) {

    // do something

}

 

是null或者空串:方法三

import org.apache.commons.lang.StringUtils;

if (StringUtils.isEmpty(s)) {

    // do something

}

 

不是null也不是空串:方法一

if (s != null && s.length() != 0) {

    // do something

}

 

不是null也不是空串:方法二

if (s != null && s.isEmpty()) {

    // do something

}

 

不是null也不是空串:方法三

import org.apache.commons.lang.StringUtils;

if (StringUtils.isNotEmpty(s)) {

    // do something

}

 

JavaDoc String.isEmpty 写道
boolean isEmpty()
Returns true if, and only if, length() is 0.
 
org.apache.commons.lang.StringUtils 写道

isEmpty

public static boolean isEmpty(String str)

    Checks if a String is empty ("") or null.

     StringUtils.isEmpty(null)      = true
     StringUtils.isEmpty("")        = true
     StringUtils.isEmpty(" ")       = false
     StringUtils.isEmpty("bob")     = false
     StringUtils.isEmpty("  bob  ") = false

    NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank().

    Parameters:
        str - the String to check, may be null
    Returns:
        true if the String is empty or null


isNotEmpty

public static boolean isNotEmpty(String str)

    Checks if a String is not empty ("") and not null.

     StringUtils.isNotEmpty(null)      = false
     StringUtils.isNotEmpty("")        = false
     StringUtils.isNotEmpty(" ")       = true
     StringUtils.isNotEmpty("bob")     = true
     StringUtils.isNotEmpty("  bob  ") = true

    Parameters:
        str - the String to check, may be null
    Returns:

        true if the String is not empty and not null
 

 

判断是否null、空串或空白串

是null或空串或空白串:方法一

if (s == null || s.length() == 0 || s.trim().length() == 0) {

    // do something

}

 

是null或空串或空白串:方法二

if (s == null || s.matches("\\s*")) {

    // do something

}

 

是null或空串或空白串:方法三

import org.apache.commons.lang.StringUtils;

if (StringUtils.isBlank(s)) {

    // do something

}

 

不是null也不是空串和空白串:方法一

if (s != null && s.length() != 0 && s.trim().length() != 0) {

    // do something

}

 

不是null也不是空串和空白串:方法二

if (s != null && !s.matches("\\s*")) {

    // do something

}

 

不是null也不是空串和空白串:方法三

import org.apache.commons.lang.StringUtils;

if (StringUtils.isNotBlank(s)) {

    // do something

}

 

org.apache.commons.lang.StringUtils 写道

isBlank

public static boolean isBlank(String str)

    Checks if a String is whitespace, empty ("") or null.

     StringUtils.isBlank(null)      = true
     StringUtils.isBlank("")        = true
     StringUtils.isBlank(" ")       = true
     StringUtils.isBlank("bob")     = false
     StringUtils.isBlank("  bob  ") = false

    Parameters:
        str - the String to check, may be null
    Returns:
        true if the String is null, empty or whitespace


isNotBlank

public static boolean isNotBlank(String str)

    Checks if a String is not empty (""), not null and not whitespace only.

     StringUtils.isNotBlank(null)      = false
     StringUtils.isNotBlank("")        = false
     StringUtils.isNotBlank(" ")       = false
     StringUtils.isNotBlank("bob")     = true
     StringUtils.isNotBlank("  bob  ") = true

    Parameters:
        str - the String to check, may be null
    Returns:
        true if the String is not empty and not null and not whitespace

 

 

In Bash

判断是否空串(或者未定义)

格式1:test -z "$STR"
格式2:[ -z "$STR" ]

注:test是[]的同义词。注意加上引号,否则有可能报错。

格式3:test "$STR" == ""

格式4:[ "$STR" == "" ]

格式5:test "$STR" = ""

格式6:[ "$STR" = "" ]

注:==等同于=。

格式7:[[ "$STR" = "" ]]

格式8:[[ $STR = "" ]]

格式9:[[ "$STR" == "" ]]

格式10:[[ $STR == "" ]]

格式11:[[ ! $STR ]]

注:[[是Bash关键字,其中的变量引用不需要加双引号。

 

[root@jfht ~]# if test -z "$STR"; then echo "STR is null or empty"; fi
STR is null or empty
[root@jfht ~]# if [ -z "$STR" ]; then echo "STR is null or empty"; fi    
STR is null or empty
[root@jfht ~]# if test "$STR" == ""; then echo "STR is null or empty"; fi      
STR is null or empty
[root@jfht ~]# if [ "$STR" == "" ]; then echo "STR is null or empty"; fi     
STR is null or empty
[root@jfht ~]# if test "$STR" = ""; then echo "STR is null or empty"; fi     
STR is null or empty
[root@jfht ~]# if [ "$STR" = "" ]; then echo "STR is null or empty"; fi    
STR is null or empty
[root@jfht ~]# if [[ "$STR" = "" ]]; then echo "STR is null or empty"; fi
STR is null or empty
[root@jfht ~]# if [[ $STR = "" ]]; then echo "STR is null or empty"; fi 
STR is null or empty
[root@jfht ~]# if [[ ! $STR ]]; then echo "STR is null or empty"; fi    
STR is null or empty
[root@jfht ~]#

 

判断是否非空串

格式1:test "$STR"

格式2:[ "$STR" ]
格式3:test -n "$STR"

格式4:[ -n "$STR" ]

格式5:test ! -z "$STR"

格式6:[ ! -z "$STR" ]

格式7:test "$STR" != ""

格式8:[ "$STR" != "" ]

格式9:[[ "$STR" ]]

格式10:[[ $STR ]]

 

man test 写道
-n STRING
   the length of STRING is nonzero

STRING
   equivalent to -n STRING

-z STRING
    the length of STRING is zero
 

 

判断变量是否已定义(声明)

格式1:if declare -p VAR; then do_something; fi

格式2:declare -p VAR && do_something

在Bash中typeset命令等同于declare命令。

格式3:if [ "${VAR+YES}" ]; then do_something; fi

格式4:[ "${VAR+YES}" ] && do_something

${VAR+YES}表示如果VAR没有定义则返回YES,否则返回空

 

[root@jfht ~]# if declare -p VAR; then echo "VAR defined"; fi
-bash: declare: VAR: not found
[root@jfht ~]# declare -p VAR && echo "VAR defined"
-bash: declare: VAR: not found
[root@jfht ~]# if [ "${VAR+YES}" ]; then echo "VAR defined"; fi
[root@jfht ~]# [ "${VAR+YES}" ] &&echo "VAR defined"
[root@jfht ~]#
[root@jfht ~]# VAR=
[root@jfht ~]# if declare -p VAR; then echo "VAR defined"; fi 
declare -- VAR=""
VAR defined
[root@jfht ~]# declare -p VAR && echo "VAR defined"            
declare -- VAR=""
VAR defined
[root@jfht ~]# if [ "${VAR+YES}" ]; then echo "VAR defined"; fi
VAR defined
[root@jfht ~]# [ "${VAR+YES}" ] &&echo "VAR defined"
VAR defined
[root@jfht ~]#

 

判断变量没有定义(声明)

格式1:if [ ! "${VAR+YES}" ]; then do_something; fi

格式2:[ ! "${VAR+YES}" ] && do_something

 

 

本文链接:http://codingstandards.iteye.com/blog/1171360   (转载请注明出处)

返回目录:Java程序员的Bash实用指南系列之字符串处理(目录) 

上节内容:Bash字符串处理(与Java对照) - 5.字符串输入(读取字符串)

下节内容:Bash字符串处理(与Java对照) - 7.字符串与默认值

 

 

5
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics