String工具类总结

/*
 * null则返回空字符串
 */
public static String isBlank(Object parameter) {
    return parameter == null ? "" : parameter.toString();
}
/**
 * 字符串正则表达式
 */
private final static String RULE = "([A-Za-z\\d]+)(_)?";

/**
 * 下划线转驼峰法
 *
 * @param line       源字符串
 * @param smallCamel 大小驼峰,是否为小驼峰
 * @return 转换后的字符串
 */
public static String underline2Camel(String line, boolean smallCamel) {
    if (org.apache.commons.lang3.StringUtils.isBlank(line)) {
        return "";
    }
    StringBuffer sb = new StringBuffer();
    Pattern pattern = Pattern.compile(RULE);
    Matcher matcher = pattern.matcher(line);
    while (matcher.find()) {
        String word = matcher.group();
        sb.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0)) : Character.toUpperCase(word.charAt(0)));
        int index = word.lastIndexOf('_');
        if (index > 0) {
            sb.append(word.substring(1, index).toLowerCase());
        } else {
            sb.append(word.substring(1).toLowerCase());
        }
    }
    return sb.toString();
}

/**
 * 驼峰法转下划线
 *
 * @param line 源字符串
 * @return 转换后的字符串
 */
public static String camel2Underline(String line) {
    if (org.apache.commons.lang3.StringUtils.isBlank(line)) {
        return "";
    }
    line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
    StringBuffer sb = new StringBuffer();
    Pattern pattern = Pattern.compile(RULE);
    Matcher matcher = pattern.matcher(line);
    while (matcher.find()) {
        String word = matcher.group();
        sb.append(word.toLowerCase());
        sb.append(matcher.end() == line.length() ? "" : "_");
    }
    return sb.toString();
}

/**
 * 判断str1是否包含str2
 *
 * @param str1
 * @param str2
 * @return
 */
public static Boolean contains(String str1, String str2) {
    if (isBlank(str1) || isBlank(str2)) {
        return false;
    } else {
        str1 = "," + str1 + ",";
        str2 = "," + str2 + ",";
        if (str1.contains(str2)) {
            return true;
        } else {
            return false;
        }

    }

}

/**
    * 转码
    * 
    * @param str
    * @return
    */
   public static String toUTF(String str) {
      if ("".equals(str) || str == null) {
         return "";
      } else {
         try {
            str = new String(str.trim().getBytes("ISO-8859-1"), "UTF-8");
         } catch (UnsupportedEncodingException e) {
            logger.error("转码失败", e);
         }
         return str;
      }
   }
   
   /**
   * 除去Html文件中的class、或style属性
   * @param str
   * @param subStr
   * @return
   */
   public static String getString(String str, String subStr){
      String sb = "";
      String[] s1 = str.split(subStr);
      for (int i = 0; i < s1.length; i++) {
          if (i==0) {
              sb = sb + s1[0];
              continue;
          }
          int ii = s1[i].indexOf(">", 0);
          sb = sb + s1[i].substring(ii);
      }
      return sb.toString();
   }
   
   /**
     * @Description: 去除html
     * @author fch
     * @date 2015-11-25
     */
    public static String getNoHTMLString(String content){
//            java.util.regex.Pattern p_script; 
//            java.util.regex.Matcher m_script; 
//            java.util.regex.Pattern p_style; 
//            java.util.regex.Matcher m_style; 
            Pattern p_html;
            java.util.regex.Matcher m_html; 
           
        try { 
            //String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
            //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> } 
           // String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; 
                  //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> } 
            
            //String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 

                  String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式 
              
                 // p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); 
                 // m_script = p_script.matcher(content); 
                 // content = m_script.replaceAll(""); //过滤script标签
            
                  //p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); 
                 // m_style = p_style.matcher(content); 
                 // content = m_style.replaceAll(""); //过滤style标签 
                  
                  p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); 
                  m_html = p_html.matcher(content); 
                  content = m_html.replaceAll(""); //过滤html标签 
              }catch(Exception e) { 
                      return "";
              } 
             return content;
       }

   /**
    * 保留两位小数,并且去尾0
    * @param db
    * @return
    */
   public static String getFormatNum(double db){
      DecimalFormat df   =new   DecimalFormat("#.00");
      String rs = Common.isBlank(df.format(db));
      rs = "".equals(rs)?"0":db<1?"0"+rs:rs;
      return rs.replace(".00", "");
   }

猜你喜欢

转载自blog.csdn.net/qq_33352259/article/details/80730173