• 有意思的String字符工具类


      对String的操作是Java攻城师必备的,一个优秀的攻城师是懒惰,他会把自己的一些常见的代码写成可提供拓展和复用的工具类或者工具库,这些是这些优秀工程师的法宝。

      我就先从String这个基本操作开始吧,Android里有个TextUtils的类,没事点开也看看。

      1 public class StringUtils {
      2     
      3     
      4     private static final String regEx_script = "<script[^>]*?>[\s\S]*?<\/script>"; // 定义script的正则表达式
      5     private static final String regEx_style = "<style[^>]*?>[\s\S]*?<\/style>"; // 定义style的正则表达式
      6     private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
      7     private static final String regEx_space = "\s*|	|
    |
    ";//定义空格回车换行符
      8     
      9     /**
     10      * @param htmlStr
     11      * @return
     12      *  删除Html标签
     13      */
     14     public static String delHTMLTag(String htmlStr) {
     15         Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
     16         Matcher m_script = p_script.matcher(htmlStr);
     17         htmlStr = m_script.replaceAll(""); // 过滤script标签
     18 
     19         Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
     20         Matcher m_style = p_style.matcher(htmlStr);
     21         htmlStr = m_style.replaceAll(""); // 过滤style标签
     22 
     23         Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
     24         Matcher m_html = p_html.matcher(htmlStr);
     25         htmlStr = m_html.replaceAll(""); // 过滤html标签
     26 
     27         Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
     28         Matcher m_space = p_space.matcher(htmlStr);
     29         htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
     30         return htmlStr.trim(); // 返回文本字符串
     31     }
     32     
     33     
     34     public static String join(String[] strs, String split) {
     35         if (strs == null || strs.length == 0) {
     36             return "";
     37         }
     38         StringBuffer sb = new StringBuffer();
     39         sb.append(strs[0]);
     40         for (int i = 1; i < strs.length; i++) {
     41             if (split != null) {
     42                 sb.append(split);
     43             }
     44 
     45             sb.append(strs[i]);
     46         }
     47 
     48         return sb.toString();
     49     }
     50 
     51     public static String validString(String str) {
     52         return TextUtils.isEmpty(str) ? "" : str;
     53     }
     54 
     55     public static String ToDBC(String input) {
     56         if (null == input) {
     57             return "";
     58         }
     59 
     60         char[] c = input.toCharArray();
     61         for (int i = 0; i < c.length; i++) {
     62             if (c[i] == 12288) {
     63                 c[i] = (char) 32;
     64                 continue;
     65             }
     66             if (c[i] > 65280 && c[i] < 65375)
     67                 c[i] = (char) (c[i] - 65248);
     68         }
     69         return new String(c);
     70     }
     71 
     72     
     73     /**
     74      * 验证输入的邮箱格式是否符合
     75      * 
     76      * @param email
     77      * @return 是否合法
     78      */
     79     public static boolean isEmail(String email) {
     80         boolean tag = true;
     81         final String pattern1 = "^([a-z0-9A-Z]+[-|\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,}$";
     82         final Pattern pattern = Pattern.compile(pattern1);
     83         final Matcher mat = pattern.matcher(email);
     84         if (!mat.find()) {
     85             tag = false;
     86         }
     87         return tag;
     88     }
     89     /**
     90      * str -- >yyyy-MM-dd
     91      * 
     92      * @param strDate
     93      * @return
     94      */
     95     @SuppressLint("SimpleDateFormat")
     96     public static Date toDate(String strDate) {
     97         Date date = null;
     98         try {
     99             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    100             date = sdf.parse(strDate);
    101         } catch (ParseException e) {
    102             System.out.println(e.getMessage());
    103         }
    104         return date;
    105 
    106     }
    107 }
  • 相关阅读:
    github上传文件让别人下载--xdd
    C#Windows Forms 使MessageBox顶层显示--xdd
    2019.7.16.5.21留念
    Fortran流程控制与逻辑运算、循环--xdd
    Fortran输入输出与声明--xdd
    c#关于数据和方法在不同类中的引用-xdd
    C#Windows Forms (Demo.SYS)--xdd
    github下载历史版本--xdd
    Matlab查看本机IP地址---xdd
    Matlab生成Word--xdd
  • 原文地址:https://www.cnblogs.com/Cyning/p/3798335.html
Copyright © 2020-2023  润新知