zoukankan      html  css  js  c++  java
  • 字符串操作工具类

    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;
        }
        
        
        
    }
  • 相关阅读:
    能ping通Linux但是ssh连不上问题解决方法
    php遍历目录与文件夹的多种方法详解
    Apache与Nginx的优缺点比较
    Apache查看连接数和限制当前的连接数
    【MySql】性能优化之分析命令
    PHP实现各种经典算法
    301、404、200、304等HTTP状态
    常用服务器资源地址集合
    关于WAMP的apache 人多了就访问非常卡的问题解决方法
    HTML基础
  • 原文地址:https://www.cnblogs.com/zt528/p/5416705.html
Copyright © 2011-2022 走看看