字符串操作工具类

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.List;

import com.sdp.core.config.Bs;

import jodd.util.StringUtil;

/**
 * 字符串操作工具类
 * @author Administrator
 *
 */
public abstract class Str {

    /**
     * 获取UTF-8编码字符串
     * @return
     */
    public static String encodeUTF8(String str){
        String result = "";
        try {
            if(StringUtil.isEmpty(str)){
                throw new Exception("转码字符串不能为空!");
            }
            result = new String(str.getBytes("ISO-8859-1"),Bs.Common.ENCODE_TYPE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * 获取UTF-8编码字符串
     * @return
     */
    public static String encodeISO88591(String str){
        String result = "";
        try {
            if(str.isEmpty()){
                throw new Exception("转码字符串不能为空!");
            }
            result = new String(str.getBytes(Bs.Common.ENCODE_TYPE),"ISO-8859-1");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * 获取系统错误信息
     * @param t
     * @return
     */
    public static String getStackTrace(Throwable t) {
        if(t != null){
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            try {
                t.printStackTrace(pw);
                return sw.toString();
            } finally {
                pw.close();
            }
        }
        return null;
    }
    
    /**
     * 获取模糊查询字符串
     * @param t
     * @return
     */
    public static String getLikeAfter(String str) {
        if(StringUtil.isEmpty(str)){
            return "%";
        }else{
            return str + "%";
        }
    }
    
    /**
     * 获取模糊查询字符串
     * @param t
     * @return
     */
    public static String getLike(String str) {
        if(StringUtil.isEmpty(str)){
            return "%";
        }else{
            return "%" + str + "%";
        }
    }
    
    /**
     * 将list集合拼接成'字符串'
     * @param lists String类型集合
     * @return
     */
    public static String getListPropStr(List<String> lists){
        String paramStrs = "";
        for(String param:lists){
             paramStrs += "'"+param.toString()+"',";
        }
        if(paramStrs.length()>0)
             paramStrs = paramStrs.substring(0,paramStrs.length()-1);
        return paramStrs;
    }
    
    /**
     * 将list集合拼接成'字符串'
     * @param lists Object对象集合
     * @return
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     */
    public static <T> String getListPropStr(List<T> lists,String prop) throws Exception{
        String paramStrs = "";
        String value = "";
        for(T t:lists){
             Field[] fields = t.getClass().getDeclaredFields();
             for(Field field : fields){
                 if(field.getName().equals(prop)){
                     field.setAccessible(true);
                     value = (String) field.get(t);
                     paramStrs += "'"+value+"',";
                     break;
                 }
             }
        }
        if(paramStrs.length()>0)
             paramStrs = paramStrs.substring(0,paramStrs.length()-1);
        return paramStrs;
    }
    
    
    
}

 

posted @ 2016-04-27 13:48  IAMME  阅读(182)  评论(0编辑  收藏  举报