判断字符串是否为空,一般我们会这么处理。
if(str== null || str.length == 0){......}
以更标准的方式去解决这些通用的问题,我们应该尽可能去利用那些已经非常成熟的第三方库,最大化的减少重复代码的编写,提高开发效率。
Apache是世界使用排名第一的Web服务器软件,Commons Lang是对JDK中java.lang包的补充,提供了各种各样的Utilities工具类,这里说说最常用的几个工具类。
版本:commons-lang3-3.1.jar
Apache Commons Lang是Apache最著名的JAVA库,它是对java.lang的很好扩展,包含了大量非常实用的工具类,其中用的最多的有StringUtils,DateUtils,NumberUtils等。之前提到的代码利用StringUtils可以改写为:
Jf(StringUtils.isBlank(str)){.............}
//isBlank
System.out.println(StringUtils.isBlank(null)); true
System.out.println(StringUtils.isBlank("")); true
System.out.println(StringUtils.isBlank(" ")); true
System.out.println(StringUtils.isBlank("\t")); true
//isEmpty
System.out.println(StringUtils.isEmpty(null)); true
System.out.println(StringUtils.isEmpty("")); true
System.out.println(StringUtils.isEmpty(" ")); false
除了Apache Commons Lang,还有一些其他的Apache库也是对JAVA本身的很好补充,如Apache Commons Collection,Apache Commons IO,Apache Commons Math