isEmpty與isBlank在StringUtils中有什么不同?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
公司主營(yíng)業(yè)務(wù):成都做網(wǎng)站、成都網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。成都創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。成都創(chuàng)新互聯(lián)推出武陵免費(fèi)做網(wǎng)站回饋大家。
我們常說的字符串為空,其實(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)于isEmpty與isBlank在StringUtils中有什么不同問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
網(wǎng)站標(biāo)題:isEmpty與isBlank在StringUtils中有什么不同
網(wǎng)頁(yè)網(wǎng)址:http://muchs.cn/article48/pdgiep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、小程序開發(fā)、域名注冊(cè)、定制網(wǎng)站、關(guān)鍵詞優(yōu)化、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)