StringHelper--封转自己的字符串工具类

来源:互联网 发布:耐高温密封条 淘宝 编辑:程序博客网 时间:2024/05/21 11:21

我们每次开发项目,都会有很多的关于字符串的处理,字符串的处理太常见了,无法避免,那么这时如果可以把常用的字符串处理封装成类,在以后的开发中应该会减少一些工作量,下面代码对一些常用的字符串处理进行了封装,如果遇上了其他的关于字符串处理可以不断加入进来。

现在已经实现的功能有:

1,判断一个字符串是否为 null 或 空串 或 空白,

2,判断一个字符串是否不是null且不是空串、不是空白,

3,判断一个字符串变量是否为 null,

4,判断一个字符串是否为 空串,

5,比较两个非空(不是null,不是空串、不是空白)字符串是否"相等",

6,比较两个字符串是否 "相等",

7,随机生成一个 32 位长度的 字符串( UUID ),

8,通过 SHA1 对字符串进行加密,

9,对 给定字符串 进行 md5 加密,(对密码进行加密使用)

10,检测邮箱合法性,

11,Double进行四舍五入,得到指定位数的小数,

12,根据Unicode编码完美的判断中文汉字和符号,

13,判断是否包含中文汉字,

14判断是否只有中文汉字,

15判断邮政编码.

代码有详细备注,不做说明。如下:

[java] view plain copy
 print?
  1. public final class StringHelper {  
  2.       
  3.     /** 
  4.      * 判断一个字符串是否为 null 或 空串 或 空白 
  5.      * @param source 需要判断的字符串 
  6.      * @return 当字符串为 null 或 为 空白、空串 时返回 true 
  7.      */  
  8.     public static boolean empty( String source ) {  
  9.         return source == null || source.trim().isEmpty()  ;  
  10.     }  
  11.       
  12.     /** 
  13.      * 判断一个字符串是否不是null且不是空串、不是空白 
  14.      * @param source 需要判断的字符串 
  15.      * @return 当 字符串是不是null且不是空串也不是空白时返回 true 
  16.      */  
  17.     public static boolean notEmpty( String source ) {  
  18.         return source != null && source.trim().length() > 0 ;  
  19.     }  
  20.       
  21.     /** 
  22.      * 判断一个字符串变量是否为 null 
  23.      * @param source 需要判断的字符串 
  24.      * @return 当 字符串变量 为 null 时返回 true 
  25.      */  
  26.     public static boolean isNull( String source ) {  
  27.         return source == null ;  
  28.     }  
  29.       
  30.     /** 
  31.      * 判断一个字符串是否为 空串 
  32.      * @param source 需要判断的字符串 
  33.      * @return 当字符串中的值是 空串 或 空白 串时返回 true 
  34.      */  
  35.     public static boolean emptyString( String source ) {  
  36.         return ( source != null ) && source.length() == source.trim().length() ;  
  37.     }  
  38.       
  39.     /** 
  40.      * 判断一个字符串是否为 空白 串 
  41.      * @param source 需要判断的字符串 
  42.      * @return 当字符串中的值是 空白 串时返回 true 
  43.      */  
  44.     public static boolean blank( String source ){  
  45.         return ( source != null ) && source.length() > source.trim().length()  ;  
  46.     }  
  47.       
  48.     /** 
  49.      * 比较两个非空(不是null,不是空串、不是空白)字符串是否"相等" 
  50.      * @param one 第一个需要比较的字符串 
  51.      * @param theOther 另一个参与比较的字符串 
  52.      * @return 当 两个字符串 都不为空串 且 内容完全一致 (剔除首尾空白后、大小写也一致)时返回 true 
  53.      */  
  54.     public static boolean equals( String one , String theOther) {  
  55.         return equals(one, theOther,true,false);  
  56.     }  
  57.       
  58.     /** 
  59.      * 比较两个字符串是否 "相等" 
  60.      * @param one 参与比较的第一个字符串 
  61.      * @param theOther 参与比较的另一个字符串 
  62.      * @param escapeSpace 是否需要剔除首尾空白 ( true 表示需要剔除首尾空白,false 表示不剔除 ) 
  63.      * @param ignoreCase 是否忽略大小写 ( true 表示忽略大小写 ,false 表示不忽略大小写 ) 
  64.      * @return 
  65.      */  
  66.     public static boolean equals( String one , String theOther , boolean escapeSpace , boolean ignoreCase) {  
  67.           
  68.         if( one == null || theOther == null ){  
  69.             return false ;  
  70.         }  
  71.           
  72.         if( escapeSpace ){  
  73.             one = one.trim();  
  74.             theOther = theOther.trim();  
  75.         }  
  76.           
  77.         return ignoreCase ? one.equalsIgnoreCase( theOther ) : one.equals( theOther ) ;  
  78.     }  
  79.       
  80.     /** 
  81.      * 随机生成一个 32 位长度的 字符串( UUID ) 
  82.      * @return 
  83.      */  
  84.     public static String random(){  
  85.         UUID uuid = UUID.randomUUID();//36位长度(包含了 四个 - )  
  86.         String uuidString = uuid.toString();  
  87.         uuidString = uuidString.replace("-""");  
  88.         uuidString = uuidString.toUpperCase();  
  89.         return uuidString;  
  90.     }  
  91.       
  92.     /** 
  93.      * 通过 SHA1 对字符串进行加密 
  94.      * @param source 
  95.      * @return 
  96.      */  
  97.     public static String sha1(String source ){  
  98.         try{  
  99.             MessageDigest md = MessageDigest.getInstance("SHA1");  
  100.             BASE64Encoder encoder = new BASE64Encoder();  
  101.             return encoder.encode( md.digest( source.getBytes() ) );  
  102.         }catch(NoSuchAlgorithmException e){  
  103.             e.printStackTrace();  
  104.             return null;  
  105.         }  
  106.     }  
  107.       
  108.     /** 
  109.      * 对 给定字符串 进行 md5 加密 
  110.      * @param source 待加密的字符串 
  111.      * @return 
  112.      */  
  113.     protected static String md524(String source ){  
  114.         try{  
  115.             MessageDigest md = MessageDigest.getInstance("MD5");  
  116.             BASE64Encoder encoder = new BASE64Encoder();  
  117.             return encoder.encode( md.digest(source.getBytes()) );  
  118.         }catch(NoSuchAlgorithmException e){  
  119.             e.printStackTrace();  
  120.             return null;  
  121.         }  
  122.     }  
  123.       
  124.     /** 
  125.      * 对字符串进行MD5加密 
  126.      * @param source 需要加密的字符串 
  127.      * @return 返回加密后的字符串 
  128.      */  
  129.     public static final String MD5(String source){  
  130.         if(source != null){  
  131.             StringBuffer md5 = new StringBuffer();  
  132.             MessageDigest md = null;  
  133.             try{  
  134.                 md = MessageDigest.getInstance("MD5");  
  135.                 md.update(source.getBytes());  
  136.                 byte[] mdBytes = md.digest();  
  137.                   
  138.                 for(int i = 0;i < mdBytes.length;i++){  
  139.                     int temp;  
  140.                     if(mdBytes[i] < 0){  
  141.                         temp = 256+mdBytes[i];  
  142.                     }else{  
  143.                         temp = mdBytes[i];  
  144.                     }  
  145.                     if(temp < 16){  
  146.                         md5.append("0");  
  147.                     }  
  148.                     md5.append(Integer.toString(temp,16 ));  
  149.                 }  
  150.             }catch(NoSuchAlgorithmException e){  
  151.                 e.printStackTrace();  
  152.             }  
  153.             return md5.toString().toUpperCase();  
  154.         }  
  155.         return null;  
  156.     }  
  157.     /** 
  158.      * 检测邮箱合法性 
  159.      *  
  160.      * @param email 
  161.      * @return 
  162.      */  
  163.     public static boolean isEmail(String email) {  
  164.         if ((email == null) || (email.trim().length() == 0)) {  
  165.             return false;  
  166.         }  
  167.         String regEx = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$";  
  168.         Pattern p = Pattern.compile(regEx);  
  169.         Matcher m = p.matcher(email.trim().toLowerCase());  
  170.    
  171.         return m.find();  
  172.     }  
  173.   
  174.    
  175.     /** 
  176.      * Double进行四舍五入 
  177.      *  
  178.      * @param v 
  179.      * @param scale 
  180.      * @return 
  181.      */  
  182.     public static double getDouble(Double v, int scale) {  
  183.    
  184.         if (scale < 0) {  
  185.             scale = 0;  
  186.         }  
  187.         BigDecimal b = new BigDecimal(v);  
  188.         BigDecimal one = new BigDecimal("1");  
  189.         return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();  
  190.     }  
  191.    
  192.     /** 
  193.      * 得到指定位数的小数 
  194.      * @param v 
  195.      * @param scale 
  196.      * @return 
  197.      */  
  198.     public static String getDecimals(Double v, int scale) {  
  199.    
  200.         return String.format("%." + String.valueOf(scale) + "f", v);  
  201.    
  202.     }  
  203.     /** 
  204.      * 根据Unicode编码完美的判断中文汉字和符号 
  205.      *  
  206.      * @param c 
  207.      * @return 
  208.      */  
  209.     private static boolean isChinese(char c) {  
  210.         Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);  
  211.         if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS  
  212.                 || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS  
  213.                 || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A  
  214.                 || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B  
  215.                 || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION  
  216.                 || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS  
  217.                 || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {  
  218.             return true;  
  219.         }  
  220.         return false;  
  221.     }  
  222.    
  223.     /** 
  224.      * 判断是否包含中文汉字 
  225.      *  
  226.      * @param strName 
  227.      * @return 
  228.      */  
  229.     public static boolean isChineseHave(String strName) {  
  230.         char[] ch = strName.toCharArray();  
  231.         for (int i = 0; i < ch.length; i++) {  
  232.             char c = ch[i];  
  233.             if (isChinese(c)) {  
  234.                 return true;  
  235.             }  
  236.         }  
  237.         return false;  
  238.     }  
  239.    
  240.     /** 
  241.      * 判断是否只有中文汉字 
  242.      *  
  243.      * @param strName 
  244.      * @return 
  245.      */  
  246.     public static boolean isChineseAll(String strName) {  
  247.         char[] ch = strName.toCharArray();  
  248.         for (int i = 0; i < ch.length; i++) {  
  249.             char c = ch[i];  
  250.             if (!isChinese(c)) {  
  251.                 return false;  
  252.             }  
  253.         }  
  254.         return true;  
  255.     }  
  256.    
  257.     /** 
  258.      * 判断邮政编码 
  259.      *  
  260.      * @param str 
  261.      * @return 
  262.      */  
  263.     public static boolean isCard(String str) {  
  264.         Pattern p = Pattern.compile("[1-9]\\d{5}(?!\\d)");  
  265.         Matcher m = p.matcher(str);  
  266.         return m.matches();  
  267.     }  
  268.    
  269. }  
原创粉丝点击