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)文章
Java SpringBoot模板引擎之 Thymeleaf入門詳解
jsp有著強大的功能,能查出一些數(shù)據(jù)轉(zhuǎn)發(fā)到JSP頁面以后,我們可以用jsp輕松實現(xiàn)數(shù)據(jù)的顯示及交互等,包括能寫Java代碼。但是,SpringBoot首先是以jar的方式,不是war;其次我們的tomcat是嵌入式的,所以現(xiàn)在默認(rèn)不支持jsp2021-10-10
Mybatis foreach用法解析--對于list和array
這篇文章主要介紹了Mybatis foreach用法解析--對于list和array,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
elasticsearch+logstash并使用java代碼實現(xiàn)日志檢索
這篇文章主要介紹了elasticsearch+logstash并使用java代碼實現(xiàn)日志檢索,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
Java的方法和this關(guān)鍵字如何理解與應(yīng)用
Java語言中的“方法”(Method)在其他語言當(dāng)中也可能被稱為“函數(shù)”(Function)。對于一些復(fù)雜的代碼邏輯,如果希望重復(fù)使用這些代碼,并且做到“隨時任意使用”,那么就可以將這些代碼放在一個大括號{}當(dāng)中,并且起一個名字。使用代碼的時候,直接找到名字調(diào)用即可2021-10-10
Java從網(wǎng)絡(luò)讀取圖片并保存至本地實例
這篇文章主要為大家詳細(xì)介紹了Java從網(wǎng)絡(luò)讀取圖片并保存至本地的實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04

