天天看点

Java实用的工具类StringUtils

org.apache.commons.lang.StringUtils中方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出NullPointerException,而是做了相应处理,例如,如果输入为null则返回也是null等)。

除了构造器,StringUtils中一共有130多个方法,并且都是static的,

所以我们可以这样调用StringUtils.xxx()。

Java web中使用方法:

pom.xml

引入依赖

版本(Jun 09, 2017)

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.6</version>
</dependency>
           

具体方法可以看文档

官方文档:

http://commons.apache.org/proper/commons-lang/javadocs/api-3.6/

http://commons.apache.org/proper/commons-lang/javadocs/api-3.6/org/apache/commons/lang3/StringUtils.html

或者开源社区对StringUtils的详细介绍

http://www.oschina.net/code/snippet_239959_8724

我认为比较常用的方法

空字符串检查

isEmpty

判断某字符串是否为空,为空的标准是

str==null

str.length()==0

StringUtils.isBlank("\t \n \f \r") = true //对于制表符、换行符、换页符和回车符

所以

isEmpty

适用与纯数据

isBlank

是用处像

XLS

文件的数据。去空格,

去空格后判断是否为

null

或者为

"" (空字符串)

isEmpty

相比

isBlank

多了去除空格的操作。

空串检查主要是判断字段串是否为空,返回的是

Boolean

类型。

更多常用方法:

https://my.oschina.net/yang2016/blog/737500