String工具类StringUtils的isEmpty与isBlank

1、StringUtils中isEmpty()与isBlank()

System.out.println(StringUtils.isEmpty(null));        //true
System.out.println(StringUtils.isEmpty(""));          //true
System.out.println(StringUtils.isEmpty("   "));       //false
System.out.println(StringUtils.isEmpty("dd"));        //false
System.out.println(StringUtils.isBlank(null));        //true
System.out.println(StringUtils.isBlank(""));          //true
System.out.println(StringUtils.isBlank("   "));       //true
System.out.println(StringUtils.isBlank("dd"));        //false 
2、
/**
     * 判断对象是否为null,不允许空白串
     *
     * @param object    目标对象类型
     * @return
     */
    public static boolean isNull(Object object){
        if (null == object) {
            return true;
        }
        if ((object instanceof String)){
            return "".equals(((String)object).trim());
        }
        return false;
    }

    /**
     * 判断对象是否不为null
     *
     * @param object
     * @return
     */
    public static boolean isNotNull(Object object){
        return !isNull(object);
    }

3、几个有用的String方法

/**
     * change UTF8 To GB2312
     * @param target
     * @return
     */
    public static final String UTF82GB2312(String target) {
        try {
            return new String(target.getBytes("UTF-8"), "gb2312");
        } catch (Exception localException) {
            System.err.println("UTF8 TO GB2312 change error!");
        }
        return null;
    }

    /**
     * change UTF8 To GBK
     * @param target
     * @return
     */
    public static final String UTF82GBK(String target) {
        try {
            return new String(target.getBytes("UTF-8"), "GBK");
        } catch (Exception localException) {
            System.err.println("UTF8 TO GBK change error!");
        }
        return null;
    }

    /**
     * change UTF8 To ISO8859-1
     * @param target
     * @return
     */
    public static final String UTF82ISO(String target) {
        try {
            return new String(target.getBytes("UTF-8"), "ISO8859-1");
        } catch (Exception localException) {
            System.err.println("UTF8 TO ISO8859-1 change error!");
        }
        return null;
    }

    /**
     * change Windows-1252 To UTF-8
     * @param target
     * @return
     */
    public static final String Windows1252UTF8(String target) {
        try {
            return new String(target.getBytes("Windows-1252"), "UTF-8");
        } catch (Exception localException) {
            System.err.println("Windows1252 To UTF8 chage error");
        }
        return null;
    }
参考文章: 点击打开链接

你可能感兴趣的:(java,string)