天天看點

Bash字元串處理(與Java對照) - 6.判斷字元串是否為空(不為空)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關鍵字,其中的變量引用不需要加雙引号。

[[email protected] ~]# if test -z "$STR"; then echo "STR is null or empty"; fi

STR is null or empty

[[email protected] ~]# if [ -z "$STR" ]; then echo "STR is null or empty"; fi    

STR is null or empty

[[email protected] ~]# if test "$STR" == ""; then echo "STR is null or empty"; fi      

STR is null or empty

[[email protected] ~]# if [ "$STR" == "" ]; then echo "STR is null or empty"; fi     

STR is null or empty

[[email protected] ~]# if test "$STR" = ""; then echo "STR is null or empty"; fi     

STR is null or empty

[[email protected] ~]# if [ "$STR" = "" ]; then echo "STR is null or empty"; fi    

STR is null or empty

[[email protected] ~]# if [[ "$STR" = "" ]]; then echo "STR is null or empty"; fi

STR is null or empty

[[email protected] ~]# if [[ $STR = "" ]]; then echo "STR is null or empty"; fi 

STR is null or empty

[[email protected] ~]# if [[ ! $STR ]]; then echo "STR is null or empty"; fi    

STR is null or empty

[[email protected] ~]#

判斷是否非空串

格式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,否則傳回空

[[email protected] ~]# if declare -p VAR; then echo "VAR defined"; fi

-bash: declare: VAR: not found

[[email protected] ~]# declare -p VAR && echo "VAR defined"

-bash: declare: VAR: not found

[[email protected] ~]# if [ "${VAR+YES}" ]; then echo "VAR defined"; fi

[[email protected] ~]# [ "${VAR+YES}" ] &&echo "VAR defined"

[[email protected] ~]#

[[email protected] ~]# VAR=

[[email protected] ~]# if declare -p VAR; then echo "VAR defined"; fi 

declare -- VAR=""

VAR defined

[[email protected] ~]# declare -p VAR && echo "VAR defined"            

declare -- VAR=""

VAR defined

[[email protected] ~]# if [ "${VAR+YES}" ]; then echo "VAR defined"; fi

VAR defined

[[email protected] ~]# [ "${VAR+YES}" ] &&echo "VAR defined"

VAR defined

[[email protected] ~]#

判斷變量沒有定義(聲明)

格式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.字元串與預設值