StringUtils里的isEmpty方法和isBlank方法的區(qū)別詳解
前言
我們常說(shuō)的字符串為空,其實(shí)就是一個(gè)沒有字符的空數(shù)組。比如:
String a = "";
a 就可以稱為是一個(gè)空字符串。由于 String 在 Java 中底層是通過 char 數(shù)組去存儲(chǔ)字符串的,所以空字符串對(duì)應(yīng)的 char 數(shù)組表現(xiàn)形式為
private final char value[] = new char[0];
但實(shí)際工作中,我們需要對(duì)字符串進(jìn)行一些校驗(yàn),比如:是否為 null,是否為空,是否去掉空格、換行符、制表符等也不為空。我們一般都是通過一些框架的工具類去做這些判斷,比如:apache 的 commons jar 包。下面就講述一下常見的兩個(gè)字符串校驗(yàn)方法以及它們的區(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é)論
通過以上代碼對(duì)比我們可以看出:
1.isEmpty 沒有忽略空格參數(shù),是以是否為空和是否存在為判斷依據(jù)。
2.isBlank 是在 isEmpty 的基礎(chǔ)上進(jìn)行了為空(字符串都為空格、制表符、tab 的情況)的判斷。(一般更為常用)
大家可以看下面的例子去體會(huì)一下。
StringUtils.isEmpty("yyy") = false StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isBlank("yyy") = false StringUtils.isBlank("") = true
實(shí)例展示
自定義判斷方法,實(shí)現(xiàn)同樣的判斷邏輯
/** * 判斷對(duì)象是否為null,不允許空白串 * * @param object 目標(biāo)對(duì)象類型 * @return */ public static boolean isNull(Object object){ if (null == object) { return true; } if ((object instanceof String)){ return "".equals(((String)object).trim()); } return false; } /** * 判斷對(duì)象是否不為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)行這些操作的簡(jiǎ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>類型的值 * * 如果傳遞過來(lái)的參數(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA 2017.1.4 x64配置步驟(介紹)
下面小編就為大家?guī)?lái)一篇IntelliJ IDEA 2017.1.4 x64配置步驟(介紹)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-06-06詳解Java程序并發(fā)的Wait-Notify機(jī)制
這篇文章主要介紹了詳解Java程序并發(fā)的Wait-Notify機(jī)制,多線程并發(fā)是Java編程中的重要部分,需要的朋友可以參考下2015-07-07Mybatis 中的sql批量修改方法實(shí)現(xiàn)
在項(xiàng)目中遇到需要批量更新的功能,原本想的是在Java中用循環(huán)訪問數(shù)據(jù)庫(kù)去更新,但是心里總覺得這樣做會(huì)不會(huì)太頻繁了,太耗費(fèi)資源了,效率也很低,查了下mybatis的批量操作,原來(lái)確實(shí)有<foreach>標(biāo)簽可以做到,下面通過本文給大家介紹下2017-01-01Spring開發(fā)核心之AOP的實(shí)現(xiàn)與切入點(diǎn)持久化
面向?qū)ο缶幊淌且环N編程方式,此編程方式的落地需要使用“類”和 “對(duì)象”來(lái)實(shí)現(xiàn),所以,面向?qū)ο缶幊唐鋵?shí)就是對(duì) “類”和“對(duì)象” 的使用,面向切面編程,簡(jiǎn)單的說(shuō),就是動(dòng)態(tài)地將代碼切入到類的指定方法、指定位置上的編程思想就是面向切面的編程2022-10-10Springboot-dubbo-fescar 阿里分布式事務(wù)的實(shí)現(xiàn)方法
這篇文章主要介紹了Springboot-dubbo-fescar 阿里分布式事務(wù)的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2019-03-03