• 字符串操作工具类


    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;
        }
        
        
        
    }
  • 相关阅读:
    Mysql5.7 单表 500万数据迁移到新表的快速实现方案
    Jmeter5 实现多机集群压测(局域网组成多机集群)
    IDEA 配置datasource,提升编码效率,让你在 Mapper.xml 中编写sql可以飞起来~
    Logstash 6.4.3 导入 csv 数据到 ElasticSearch 6.4.3
    从零开始搭建一个从Win7环境备份至CentOS7的SVN双机备份环境
    实现logstash6.4.3 同步mysql数据到Elasticsearch6.4.3
    uEditor使用步骤
    动态添加a标签打开新网页
    vue给对象添加新的响应式属性
    vue下载excel
  • 原文地址:https://www.cnblogs.com/zt528/p/5416705.html
Copyright © 2020-2023  润新知