當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring工具类的使用
生活随笔
收集整理的這篇文章主要介紹了
Spring工具类的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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[] | 解碼 |
?
轉載于:https://my.oschina.net/u/1399599/blog/915568
總結
以上是生活随笔為你收集整理的Spring工具类的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python实现答题程序
- 下一篇: JavaScriptSerializer