StringUtil

import org.apache.commons.lang.StringUtils;

public class StringUtil extends org.apache.commons.lang3.StringUtils {

    public static StringBuilder deleteLastSequence(StringBuilder sb, String suffix) {
        String sql = "";
        if (sb.lastIndexOf(suffix) != -1) {
            sql = sb.substring(0, sb.lastIndexOf(suffix));
        }
        return sb.delete(0, sb.length()).append(sql);
    }
    
    public static String parseObjectArray(Object[] arrays){
        StringBuffer sb = new StringBuffer();
        for (Object obj : arrays){
            sb.append(obj).append(",");
        }
        if (sb.length() > 0){
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
    public static String convertObj2String(Object obj){
        if (null != obj){
            if (obj.getClass().isAssignableFrom(String.class)){
                String str = obj.toString().trim();
                if (StringUtils.isBlank(str)){
                    return StringUtils.EMPTY;
                }
                
                return filterSpecialCharOfXml(str);
            } else {
                return convertObj2String(obj.toString());
            }
        } else {
            return StringUtils.EMPTY;
        }
    }
    private static String filterSpecialCharOfXml(String txt){
        String res = StringUtils.EMPTY;
        for (int i = 0; i < txt.length(); ++i) {
            char ch = txt.charAt(i);
            if (Character.isDefined(ch) && ch != '&' && ch != '<' && ch != '>'
                    && ch != '\"' && ch != '\'') {
                res = res + ch;
            }
        }
        return res;
    }
}

 

posted @ 2017-03-16 14:43  tonggc1668  阅读(194)  评论(0编辑  收藏  举报