几种判断整数和字符串的方法

来源:互联网 发布:oracle数据库重启命令 编辑:程序博客网 时间:2024/05/22 06:06

原文地址

http://www.blogjava.net/rain1102/archive/2006/10/18/75880.html

 

//判断是否是整数

// 方法1
 public static boolean isNumeric(String s)
 {
  if((s != null)&&(s!=""))
   return s.matches("^[0-9]*$");
  else
   return false;
 }

// 方法2

 public static boolean isNumeric(Object obj){
      Pattern pattern = Pattern.compile("[0-9]*");
      return pattern.matcher(obj.toString()).matches();   
 }
//判断传递来的是否是数字
 public static int checkID(String s)
 {
  if((s == null)||(s.length() == 0)||!s.matches("^[0-9]*$"))
  {
   return 0;
  }
  else
  {
   if(s.length() < 10)
   {
     return Integer.parseInt(s);
      }
      else
      {
        return 0;
      }
  }
 }
//判断传递来的是否是字符串
  public static String checkString(String s)
 {
  if((s == null)||(s.length() == 0)||s.matches("^[0-9]*$"))
  {
   return "";
  }
  else
  {
   return s;    
  }
 }