In Java
Judge whether it is null
Is null
If (s = null ){
// Do something
}
Not null
If (s! = Null ){
// Do something
}
Determines whether null or empty string
Is null or empty string: method 1
If (s = null | s. length () = 0 ){
// Do something
}
Is null or empty string: method 2
If (s = null | s. isEmpty ()){
// Do something
}
Is null or empty string: method 3
Import org. apache. commons. lang. StringUtils;
If (StringUtils. isEmpty (s )){
// Do something
}
Neither null nor empty string: method 1
If (s! = Null & s. length ()! = 0 ){
// Do something
}
Neither null nor empty string: method 2
If (s! = Null & s. isEmpty ()){
// Do something
}
Neither null nor empty string: method 3
Import org. apache. commons. lang. StringUtils;
If (StringUtils. isNotEmpty (s )){
// Do something
}
JavaDoc String. isEmpty writes
Boolean isEmpty ()
Returns true if, and only if, length () is 0.
Org. apache. commons. lang. StringUtils wrote
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
Determines whether a null, empty, or blank string is used.
Is null, empty, or blank: method 1
If (s = null | s. length () = 0 | s. trim (). length () = 0 ){
// Do something
}
Is null, empty, or blank: method 2
If (s = null | s. matches ("\ s *")){
// Do something
}
Is null, empty, or blank: method 3
Import org. apache. commons. lang. StringUtils;
If (StringUtils. isBlank (s )){
// Do something
}
Neither null nor empty or blank: method 1
If (s! = Null & s. length ()! = 0 & s. trim (). length ()! = 0 ){
// Do something
}
Neither null nor empty or blank: method 2
If (s! = Null &&! S. matches ("\ s *")){
// Do something
}
Neither null nor empty or blank: method 3
Import org. apache. commons. lang. StringUtils;
If (StringUtils. isNotBlank (s )){
// Do something
}
Org. apache. commons. lang. StringUtils wrote
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
Determines whether the string is empty (or undefined)
Format 1: test-z "$ STR"
Format 2: [-z "$ STR"]
Note: test is a synonym. Note that quotation marks are added. Otherwise, an error may be reported.
Format 3: test "$ STR" = ""
Format 4: ["$ STR" = ""]
Format 5: test "$ STR" = ""
Format 6: ["$ STR" = ""]
Note: = is equivalent to =.
Format 7: [["$ STR" = ""]
Format 8: [[$ STR = ""]
Format 9: [["$ STR" = ""]
Format 10: [[$ STR = ""]
Format 11 :[[! $ STR]
Note: [[it is a Bash keyword, where variable reference does not require double quotation marks.
[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 ~] #
Judge whether the string is non-empty
Format 1: test "$ STR"
Format 2: ["$ STR"]
Format 3: test-n "$ STR"
Format 4: [-n "$ STR"]
Format 5: test! -Z "$ STR"
Format 6 :[! -Z "$ STR"]
Format 7: test "$ STR "! = ""
Format 8: ["$ STR "! = ""]
Format 9: [["$ STR"]
Format 10: [[$ STR]
Man test wrote:
-N STRING
The length of STRING is nonzero
STRING
Equivalent to-n STRING
-Z STRING
The length of STRING is zero
Determine whether the variable has been defined (declared)
Format 1: if declare-p VAR; then do_something; fi
Format 2: declare-p VAR & do_something
In Bash, the typeset command is equivalent to the declare command.
Format 3: if ["$ {VAR + YES}"]; then do_something; fi
Format 4: ["$ {VAR + YES}"] & do_something
$ {VAR + YES} indicates that if VAR is not defined, YES is returned; otherwise, null is returned.
[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 ~] #
Variable definition (Declaration)
Format 1: if [! "$ {VAR + YES}"]; then do_something; fi
Format 2 :[! "$ {VAR + YES}"] & do_something
Author: "Bash @ Linux"