StringUtils里的isEmpty方法和isBlank方法的區(qū)別詳解
前言
我們常說的字符串為空,其實就是一個沒有字符的空數(shù)組。比如:
String a = "";
a 就可以稱為是一個空字符串。由于 String 在 Java 中底層是通過 char 數(shù)組去存儲字符串的,所以空字符串對應(yīng)的 char 數(shù)組表現(xiàn)形式為
private final char value[] = new char[0];
但實際工作中,我們需要對字符串進(jìn)行一些校驗,比如:是否為 null,是否為空,是否去掉空格、換行符、制表符等也不為空。我們一般都是通過一些框架的工具類去做這些判斷,比如:apache 的 commons jar 包。下面就講述一下常見的兩個字符串校驗方法以及它們的區(qū)別。
isEmpty()
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
isBlank()
public static boolean isBlank(String str) {
int strLen;
if (str != null && (strLen = str.length()) != 0) {
for(int i = 0; i < strLen; ++i) {
// 判斷字符是否為空格、制表符、tab
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
return true;
} else {
return true;
}
}
結(jié)論
通過以上代碼對比我們可以看出:
1.isEmpty 沒有忽略空格參數(shù),是以是否為空和是否存在為判斷依據(jù)。
2.isBlank 是在 isEmpty 的基礎(chǔ)上進(jìn)行了為空(字符串都為空格、制表符、tab 的情況)的判斷。(一般更為常用)
大家可以看下面的例子去體會一下。
StringUtils.isEmpty("yyy") = false
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isBlank("yyy") = false
StringUtils.isBlank("") = true
實例展示
自定義判斷方法,實現(xiàn)同樣的判斷邏輯
/**
* 判斷對象是否為null,不允許空白串
*
* @param object 目標(biāo)對象類型
* @return
*/
public static boolean isNull(Object object){
if (null == object) {
return true;
}
if ((object instanceof String)){
return "".equals(((String)object).trim());
}
return false;
}
/**
* 判斷對象是否不為null
*
* @param object
* @return
*/
public static boolean isNotNull(Object object){
return !isNull(object);
}
System.out.println(StringHandler.isNull(null)); //true
System.out.println(StringHandler.isNull("")); //true
System.out.println(StringHandler.isNull(" ")); //true
System.out.println(StringHandler.isNull("dd")); //false
通常我們通過HttpServletRequest獲取到的參數(shù),需要經(jīng)過判空處理,轉(zhuǎn)型然后得到我們想要的值,這里可以進(jìn)行這些操作的簡單封裝.如下
/**
* 從<code>HttpServletRequest</code>中獲取<code>String</code>類型的值, 不允許傳遞空串
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @return
* 返回需要的值
*/
public static final String getString(HttpServletRequest request,String paramName){
return getString(request, paramName, false);
}
/**
* 從<code>HttpServletRequest</code>中獲取<code>String</code>類型的值
*
* 如果傳遞過來的參數(shù)為包含空白字符串的字符,認(rèn)為為有效值, 否則返回null
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @return
* 返回需要的值
*/
public static final String getString(HttpServletRequest request,String paramName,boolean isWithSpace) {
String tmp = request.getParameter(paramName);
if(isWithSpace){
//如果允許包含空格,則使用isEmpty判空
if (!StringUtils.isEmpty(tmp)){
return tmp;
}
}else{
if(!StringUtils.isBlank(tmp)){
return tmp;
}
}
return null;
}
/**
* 從<code>HttpServletRequest</code>中獲取<code>Long</code>類型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @return
* 返回需要的值
*/
public static final Long getLong(HttpServletRequest request,String paramName) {
return getLong(request, paramName, -1L);
}
/**
* 從<code>HttpServletRequest</code>中獲取<code>Long</code>類型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @param defaultValue
* 默認(rèn)值
* @return
* 返回需要的值
*/
public static final Long getLong(HttpServletRequest request,String paramName,Long defaultValue) {
String tmp = request.getParameter(paramName);
if (!StringUtils.isBlank(tmp)){
try {
Long value = Long.parseLong(tmp);
return value;
} catch (NumberFormatException e) {
return -1L;
}
}
return defaultValue;
}
/**
* 從<code>HttpServletRequest</code>中獲取<code>Integer</code>類型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @return
* 返回需要的值
*/
public static final Integer getInt(HttpServletRequest request,String paramName) {
return getInt(request,paramName, -1);
}
/**
* 從<code>HttpServletRequest</code>中獲取<code>Integer</code>類型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @param defaultValue
* 默認(rèn)值
* @return
* 返回需要的值
*/
public static final Integer getInt(HttpServletRequest request,String paramName, int defaultValue) {
String tmp = request.getParameter(paramName);
if (!StringUtils.isBlank(tmp)){
try {
Integer value = Integer.parseInt(tmp);
return value;
} catch (NumberFormatException e) {
return -1;
}
}
return defaultValue;
}
/**
* 從<code>HttpServletRequest</code>中獲取<code>Short</code>類型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @return
* 返回需要的值
*/
public static final Short getShort(HttpServletRequest request,String paramName) {
return getShort(request,paramName, (short)-1);
}
/**
* 從<code>HttpServletRequest</code>中獲取<code>Short</code>類型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @param defaultValue
* 默認(rèn)值
* @return
* 返回需要的值
*/
public static final Short getShort(HttpServletRequest request,String paramName, short defaultValue) {
String tmp = request.getParameter(paramName);
if (!StringUtils.isBlank(tmp)){
try {
Short value = Short.parseShort(tmp);
return value;
} catch (NumberFormatException e) {
return (short)-1;
}
}
return defaultValue;
}
/**
* 從<code>HttpServletRequest</code>中獲取<code>Byte</code>類型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @return
* 返回需要的值
*/
public static final Byte getByte(HttpServletRequest request,String paramName) {
return getByte(request,paramName, (byte)-1);
}
/**
* 從<code>HttpServletRequest</code>中獲取<code>Byte</code>類型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @param defaultValue
* 默認(rèn)值
* @return
* 返回需要的值
*/
public static final Byte getByte(HttpServletRequest request,String paramName, Byte defaultValue) {
String tmp = request.getParameter(paramName);
if (!StringUtils.isBlank(tmp)){
try {
Byte value = Byte.parseByte(tmp);
return value;
} catch (NumberFormatException e) {
return (byte)-1;
}
}
return defaultValue;
}
/**
* 從<code>HttpServletRequest</code>中獲取<code>Double</code>類型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @return
* 返回需要的值
*/
public static final Double getDouble(HttpServletRequest request,String paramName) {
return getDouble(request, paramName,-1D);
}
/**
* 從<code>HttpServletRequest</code>中獲取<code>Double</code>類型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @param defaultValue
* 默認(rèn)值
* @return
* 返回需要的值
*/
public static final Double getDouble(HttpServletRequest request,String paramName, Double defaultValue) {
String tmp = request.getParameter(paramName);
if (!StringUtils.isBlank(tmp)){
try {
Double value = Double.parseDouble(tmp);
return value;
} catch (NumberFormatException e) {
return -1D;
}
}
return defaultValue;
}
/**
* 從<code>HttpServletRequest</code>中獲取<code>Float</code>類型的值
*
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @return
* 返回需要的值
*/
public static final Float getFloat(HttpServletRequest request,String paramName) {
return getFloat(request, paramName,-1F);
}
/**
* 從<code>HttpServletRequest</code>中獲取<code>Float</code>類型的值
*
* @param request
* @see HttpServletRequest
* @param paramName
* 參數(shù)名稱
* @param defaultValue
* 默認(rèn)值
* @return
* 返回需要的值
*/
public static final Float getFloat(HttpServletRequest request,String paramName, Float defaultValue) {
String tmp = request.getParameter(paramName);
if (!StringUtils.isBlank(tmp)){
try {
Float value = Float.parseFloat(tmp);
return value;
} catch (NumberFormatException e) {
return -1F;
}
}
return defaultValue;
}
到此這篇關(guān)于StringUtils里的isEmpty方法和isBlank方法的區(qū)別詳解的文章就介紹到這了,更多相關(guān)StringUtils isEmpty isBlank 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA 2017.1.4 x64配置步驟(介紹)
下面小編就為大家?guī)硪黄狪ntelliJ IDEA 2017.1.4 x64配置步驟(介紹)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
詳解Java程序并發(fā)的Wait-Notify機(jī)制
這篇文章主要介紹了詳解Java程序并發(fā)的Wait-Notify機(jī)制,多線程并發(fā)是Java編程中的重要部分,需要的朋友可以參考下2015-07-07
Spring開發(fā)核心之AOP的實現(xiàn)與切入點持久化
面向?qū)ο缶幊淌且环N編程方式,此編程方式的落地需要使用“類”和 “對象”來實現(xiàn),所以,面向?qū)ο缶幊唐鋵嵕褪菍?nbsp;“類”和“對象” 的使用,面向切面編程,簡單的說,就是動態(tài)地將代碼切入到類的指定方法、指定位置上的編程思想就是面向切面的編程2022-10-10
Springboot-dubbo-fescar 阿里分布式事務(wù)的實現(xiàn)方法
這篇文章主要介紹了Springboot-dubbo-fescar 阿里分布式事務(wù)的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03

