`
JaHunter
  • 浏览: 89536 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

常用验证方法

    博客分类:
  • java
 
阅读更多
1.正则表达式
 
      /**
       * 判断字符串是否为纯数字格式
       * @param num
       * @return
       * @throws BizException
       */
      public static boolean verifyNum(String num) throws BizException {
             if (num != null && !num.trim().equals( "")) {
                  Pattern p = Pattern. compile("^[0-9]*$");
                  Matcher m = p.matcher(num);
                   boolean b = m.matches();
                   if (b) {
                         return true;
                  }
            }
             return false;
      }
      
      /**
       * 判断字符串是否为纯字母格式
       * @param num
       * @return
       * @throws BizException
       */
      public static boolean verifyLetter(String str) throws BizException {
             if (str != null && !str.trim().equals( "")) {
                  Pattern p = Pattern. compile("^[A-Za-z]+$");
                  Matcher m = p.matcher(str);
                   boolean b = m.matches();
                   if (b) {
                         return true;
                  }
            }
             return false;
      }
      
      /**
       * 判断字符串是否为纯字母和数字格式
       * @param num
       * @return
       * @throws BizException
       */
      public static boolean verifyLetterAndNum(String str) throws BizException {
             if (str != null && !str.trim().equals( "")) {
                  Pattern p = Pattern. compile("^[A-Za-z0-9]+$");
                  Matcher m = p.matcher(str);
                   boolean b = m.matches();
                   if (b) {
                         return true;
                  }
            }
             return false;
      }
 
2.判断字符串是否包含汉字
public class Test {
 
      public static void main(String[] args) {
            // 方法一:
           String s1 = "我是中国人" ;
           String s2 = "imchinese" ;
           String s3 = "im中国人" ;
            System. out.println(s1 + ":" + new String(s1).length());
            System. out.println(s2 + ":" + new String(s2).length());
            System. out.println(s3 + ":" + new String(s3).length());
            System. out.println((s1.getBytes(). length == s1.length()) ? "s1无汉字"
                     : "s1有汉字" );
            System. out.println((s2.getBytes(). length == s2.length()) ? "s2无汉字"
                     : "s2有汉字" );
            System. out.println((s3.getBytes(). length == s3.length()) ? "s3无汉字"
                     : "s3有汉字" );
 
            // 方法二:
            int count = 0;
           String regEx = "[\\u4e00-\\u9fa5]" ;
           String str = "中文fd我是中国人as " ;
           Pattern p = Pattern. compile(regEx);
           Matcher m = p.matcher(str);
            while (m.find()) {
                 for ( int i = 0; i <= m.groupCount(); i++) {
                     count = count + 1;
                }
           }
            System. out.println( "共有 " + count + "个 " );
     }
}

 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics