2019獨角獸企業重金招聘Python工程師標準>>>
Spring-core中提供了大量的工具類,常用的有StringUtils
、ObjectUtils
、NumberUtils
、Base64Utils
等,Spring工具類在spring-core.jar
中的org.springframework.util
包下。
?
org.springframework.util.ObjectUtils的
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 |
?
空白字符是指空格、制表符(\t)回車符(\n)或換行符(\r)
StringUtils.isEmpty("") //true
StringUtils.isEmpty(null) //true
StringUtils.isEmpty("0") //false//--------------------------------
StringUtils.hasLength(null) = false
StringUtils.hasLength("") = false
StringUtils.hasLength(" ") = true
StringUtils.hasLength("Hello") = true//--------------------------------
StringUtils.hasText(null) = false
StringUtils.hasText("") = false
StringUtils.hasText(" ") = false
StringUtils.hasText("12345") = true
StringUtils.hasText(" 12345 ") = true//--------------------------------
StringUtils.containsWhitespace(null)=false;
StringUtils.containsWhitespace("")=false;
StringUtils.containsWhitespace("a")=false;
StringUtils.containsWhitespace("a b")=true
?
判斷返回的List和map或者其他是否為空
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) | ? |
NumberUtils
方法名 | 返回值類型 | 作用 |
---|---|---|
convertNumberToTargetClass(Number number, Class targetClass) | <T extends Number> T | 將Number轉為指定的類型 |
parseNumber(String text, Class targetClass) | <T extends Number> T | 將字符串轉為數值類型 |
parseNumber(String text, Class targetClass, NumberFormat numberFormat) | <T extends Number> T | 將字符串轉為數值類型 |
Base64Utils
方法名 | 返回值類型 | 作用 |
---|---|---|
encode(byte[] src) | byte[] | 編碼 |
decode(byte[] src) | byte[] | 解碼 |
?