spring判空工具類 spring工具

2021-10-13 14:42:11 字數 1975 閱讀 7553

spring工具類

spring-core中提供了大量的工具類,常用的有stringutils、objectutils、numberutils、base64utils等,spring工具類在spring-core.jar中的org.springframework.util包下。

1:stringutils

方法名 返回值型別 作用 備註

isempty(object str) boolean 判斷字串是否為null或者空字串 null和''都為true

haslength(charsequence str) boolean 判斷字串長度是否大於1 null和''都為false

hastext(charsequence str) boolean 判斷字串中是否有字元 null和空字白符都為false

containswhitespace(charsequence str) boolean 字串中是否含有空白字元

trimwhitespace(charsequence str) string 去掉字串中首尾的空白字元

trimallwhitespace(string str) string 去 掉字串中所有的空白字元

trimleadingwhitespace(string str) string 去掉字串左邊的空白字元

trimtrailingwhitespace(string str) string 去掉字串右邊邊的空白字元

startswithignorecase(string str, string prefix) string 判斷字串是否以xx開頭,並且忽略大小寫

getfilename(string path) string 獲取檔名 「mypath/myfile.txt」 -> 「myfile.txt」

getfilenameextension(string path) string 獲取副檔名 「mypath/myfile.txt」 -> 「txt」

stripfilenameextension(string path) string 去掉副檔名 「mypath/myfile.txt」 -> 「mypath/myfile」

replace(string instring, string oldpattern, string newpattern) string 替換字串

delete(string instring, string pattern) string 從給定的字串中刪除所有匹配的字元

deleteany(string instring, string charstodelete) string 刪除所有指定字元 「az\n」 will delete 『a』s, 『z』s and new lines

2:objectutils

isempty(object obj) boolean 判斷物件是否為空 物件為null或者陣列map為空等都為true

isempty(object array) boolean 判斷陣列是否為空

isarray(object obj) boolean 判斷物件是否為陣列

containselement(object array, object element) boolean 判斷資料組中是否包含給定的元素

addobjecttoarray(a array, o obj)

3:numberutils

convertnumbertotargetclass(number number, class targetclass) t 將number轉為指定的型別

parsenumber(string text, class targetclass) t 將字串轉為數值型別

parsenumber(string text, class targetclass, numberformat numberformat) t 將字串轉為數值型別

4:base64utils

encode(byte src) byte 編碼

decode(byte src) byte 解碼

Java中的集合類與物件的判空

1.集合類array list map set的判空 a.list進行判空 判斷關聯的積分項是否為空,若不為空,則插入資料庫 if pointsitemlist null pointsitemlist.isempty null 與 isempty 就如前輩所說的杯子與水的道理 null是為了判斷有沒...

Spring中的工具類

spring的工具模擬較實用噢,這裡僅僅只舉幾個例子 package com.itheima.sh.test public abstract class patternmatchutils int firstindex pattern.indexof if firstindex 1 if first...

Spring 資源訪問工具類

jdk所提供的訪問資源的類並不能很好的滿足各種底層資源的訪問需求,因此,spring設計了乙個resource介面,它為應用提供了更強大的訪問底層資源的能力 主要方法 boolean exists 判斷資源是否存在 boolean isopen 判斷資源是否已經開啟 url geturl 如果底層資...