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

2014-11-24 02:38:33 · 作者: · 浏览: 0

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 "$S