牛骨文教育服务平台(让学习变的简单)
博文笔记

13、Spring工具类的使用

创建时间:2016-05-22 投稿人: 浏览次数:6264

Spring-core中提供了大量的工具类,常用的有StringUtilsObjectUtilsNumberUtilsBase64Utils等,Spring工具类在spring-core.jar中的org.springframework.util包下。

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 ” will delete ‘a’s, ‘z’s and new lines


空白字符是指空格、制表符( )回车符( )或换行符( )

这里写图片描述

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

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[] 解码
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。