幾種判斷整數和字符串的方法

原文地址

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;    
  }
 }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章