commons-lang3-3.2.jar中的常用工具类的使用

  

  这个包中的很多工具类可以简化我们的操作,在这里简单的研究其中的几个工具类的使用。

1.StringUtils工具类

  可以判断是否是空串,是否为null等操作:

    /**
     * StringUtils
     */
    public static  void test1(){
        System.out.println(org.apache.commons.lang3.StringUtils.isBlank("   "));//true
        System.out.println(org.apache.commons.lang3.StringUtils.isBlank("null"));//false
        System.out.println(org.apache.commons.lang3.StringUtils.isAllLowerCase("null"));//t
        System.out.println(org.apache.commons.lang3.StringUtils.isAllUpperCase("XXXXXX"));//t
        System.out.println(org.apache.commons.lang3.StringUtils.isEmpty(" "));//f---为null或者""返回true
    }

简单的贴出几个源码便于记录:

    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

  

  CharSequence是一个接口,String,StringBuffer,StringBuilder等都实现了此接口

public abstract interface CharSequence {
    public abstract int length();

    public abstract char charAt(int paramInt);

    public abstract CharSequence subSequence(int paramInt1, int paramInt2);

    public abstract String toString();
}

2.StringEscapeUtils----------转义字符串的工具类

    /**
     * StringEscapeUtils
     */
    public static  void test2(){
        //1.防止sql注入------原理是将'替换为''
        System.out.println(org.apache.commons.lang.StringEscapeUtils.escapeSql("sss"));
        //2.转义/反转义html
        System.out.println( org.apache.commons.lang.StringEscapeUtils.escapeHtml("<a>dddd</a>"));   //&lt;a&gt;dddd&lt;/a&gt;
        System.out.println(org.apache.commons.lang.StringEscapeUtils.unescapeHtml("&lt;a&gt;dddd&lt;/a&gt;"));  //<a>dddd</a>
        //3.转义/反转义JS
        System.out.println(org.apache.commons.lang.StringEscapeUtils.escapeJavaScript("<script>alert('1111')</script>"));   
        //4.把字符串转为unicode编码
        System.out.println(org.apache.commons.lang.StringEscapeUtils.escapeJava("中国"));   
        System.out.println(org.apache.commons.lang.StringEscapeUtils.unescapeJava("\u4E2D\u56FD"));  
        //5.转义JSON
        System.out.println(org.apache.commons.lang3.StringEscapeUtils.escapeJson("{name:'qlq'}"));   
    }
扫描二维码关注公众号,回复: 2738936 查看本文章

3.NumberUtils--------字符串转数据或者判断字符串是否是数字常用工具类

    /**
     * NumberUtils
     */
    public static  void test3(){
        System.out.println(NumberUtils.isNumber("231232.8"));//true---判断是否是数字
        System.out.println(NumberUtils.isDigits("2312332.5"));//false,判断是否是整数
        System.out.println(NumberUtils.toDouble(null));//如果传的值不正确返回一个默认值,字符串转double,传的不正确会返回默认值
        System.out.println(NumberUtils.createBigDecimal("333333"));//字符串转bigdecimal
    }

4.BooleanUtils------------判断Boolean类型工具类

    /**
     * BooleanUtils
     */
    public static  void test4(){
        System.out.println(BooleanUtils.isFalse(true));//false
        System.out.println(BooleanUtils.toBoolean("yes"));//true
        System.out.println(BooleanUtils.toBooleanObject(0));//false
        System.out.println(BooleanUtils.toStringYesNo(false));//no
        System.out.println(BooleanUtils.toBooleanObject("ok", "ok", "error", "null"));//true-----第一个参数是需要验证的字符串,第二个是返回true的值,第三个是返回false的值,第四个是返回null的值
    }

5.SystemUtils----获取系统信息(原理都是调用System.getProperty())

    /**
     * SystemUtils
     */
    public static  void test5(){
        System.out.println(SystemUtils.getJavaHome());
        System.out.println(SystemUtils.getJavaIoTmpDir());
        System.out.println(SystemUtils.getUserDir());
        System.out.println(SystemUtils.getUserHome());
        System.out.println(SystemUtils.JAVA_VERSION);
        System.out.println(SystemUtils.OS_NAME);
        System.out.println(SystemUtils.USER_TIMEZONE);
    }

  此包还有好多反射用的工具类和数组工具类等,在用到的时候在使用即可。

  

猜你喜欢

转载自www.cnblogs.com/qlqwjy/p/9467178.html
今日推荐