• 字符串操作工具类


    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;
        }
        
        
        
    }
  • 相关阅读:
    Windows Mobile 6 sdk installation error, COM3 in use,please check the implementation
    使用lock_sga和pre_page_sga参数保证SGA常驻物理内存 .
    Davinci开发板DM368 nandwrite.c简要分析
    归并排序的实现
    ASP无惧上传类不能上传中文双引号文件及ASP函数InStr存在bug
    cocos2d-x绑定ccb文件
    产品经理的工作感想(3)
    9年经验,总结SEO职业瓶颈
    关于C++中的拷贝构造函数和赋值函数
    实现怎样支持Android重力感应器Sensor编程
  • 原文地址:https://www.cnblogs.com/zt528/p/5416705.html
Copyright © 2020-2023  润新知