StringUtils是类是String类的工具类,它源自:org.apache.commons.lang3.StringUtils;包下,
org.apache.commons.lang.StringUtils中方法的操作对象是java.lang.String类型的对象,
是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出NullPointerException,
而是做了相应处理,例如,如果输入为null则返回也是null等,具体可以查看源代码)。
除了构造器,StringUtils中一共有130多个方法,并且都是static的,
所以我们可以这样调用StringUtils.xxx()。
期中几个比较常用的操作比较好用,其他的没介绍的跟String的用法大同小异差不多
package com.wyw; import org.apache.commons.lang3.StringUtils; /** * org.apache.commons.lang.StringUtils中方法的操作对象是java.lang.String类型的对象, * 是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出NullPointerException, * 而是做了相应处理,例如,如果输入为null则返回也是null等,具体可以查看源代码)。 * *除了构造器,StringUtils中一共有130多个方法,并且都是static的, * *所以我们可以这样调用StringUtils.xxx()。 */ public class MyStringUtils { public static void main(String[] args) { //判断某字符串是否为空,为空的标准是str == null 或 str.length() == 0 System.out.println(StringUtils.isEmpty(null)); //true System.out.println(StringUtils.isEmpty("")); //true System.out.println(StringUtils.isEmpty(" ")); //false System.out.println(StringUtils.isEmpty(" ")); //false System.out.println("---------------------------------------------"); //判断某字符串是否非空,等于!isEmpty(String str) System.out.println(StringUtils.isNotEmpty(null)); //false System.out.println(StringUtils.isNotEmpty("")); //false System.out.println(StringUtils.isNotEmpty(" ")); //true System.out.println("---------------------------------------------"); //判断某字符串是否为空或长度为0或由空白符(whitespace)构成 System.out.println(StringUtils.isBlank(null)); //true System.out.println(StringUtils.isBlank("")); //true System.out.println(StringUtils.isBlank(" ")); //true System.out.println(StringUtils.isBlank(" r ")); //false System.out.println("---------------------------------------------"); //判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成, System.out.println(StringUtils.isNotBlank(null)) ; //false System.out.println(StringUtils.isNotBlank("")); //false System.out.println(StringUtils.isNotBlank(" ")); //false System.out.println("---------------------------------------------"); //去掉字符串两端的控制符(control characters, char <= 32) System.out.println(StringUtils.trim("")); //"" System.out.println(StringUtils.trim(null)); //null System.out.println(StringUtils.trim(" ")); //"" System.out.println(StringUtils.trim(" d s ")); //"d s" System.out.println("---------------------------------------------"); //去掉两端的空符,如果为null或者为""或者为" "均返回null System.out.println(StringUtils.trimToNull(null)); //null System.out.println(StringUtils.trimToNull("")); //null System.out.println(StringUtils.trimToNull(" ")); //null System.out.println("---------------------------------------------"); //去掉两端的空符,如果为null或者为""或者为" "均返回"" System.out.println(StringUtils.trimToEmpty("").equals("")); //true System.out.println(StringUtils.trimToEmpty(null).equals("")); //true System.out.println(StringUtils.trimToEmpty(" ").equals("")); //true System.out.println("---------------------------------------------"); //判断两个字符串是否相等 System.out.println(StringUtils.equals("","")); //true System.out.println(StringUtils.equals(null,null)); //true System.out.println(StringUtils.equals(null,"")); //false System.out.println("---------------------------------------------"); //返回字符searchChar在字符串str中第一次出现的位置。如果不存在返回-1 System.out.println(StringUtils.indexOf("abcdecc","c")); //2 System.out.println(StringUtils.indexOf("abcdecc","x")); //-1 System.out.println("---------------------------------------------"); //判断字符串str中是否包含字符searchChar。 如果str为null或"",返回false; 如果searchChar不在str中,返回false。 System.out.println(StringUtils.contains("","a")); //false System.out.println(StringUtils.contains(null,"a")); //false System.out.println("---------------------------------------------"); //字符串截取 System.out.println(StringUtils.substring("",5)); //"" System.out.println(StringUtils.substring(null,5)); //true /*表示从第5位可是截取到末尾*/ System.out.println(StringUtils.substring("abcdefjhi",5)); //"fjhi" System.out.println(StringUtils.substring("abcdef",1,3)); //bc //去掉字符串中包含remove的部分 System.out.println(StringUtils.remove("abcde","bc")); //ade } }
本节完