歡迎您光臨本站 註冊首頁

JAVA判斷字元串是否為數字

←手機掃碼閱讀     火星人 @ 2014-03-12 , reply:0
  
1.Java自帶的函數

  public static boolean isNumeric(String str) {
  for (int i = str.length(); --i >= 0;) {
   if (!Character.isDigit(str.charAt(i))) {
  return false;
  }
   }
  return true;


   }

  2.正則表達式
  public static boolean isNumeric(String str) {
   Pattern pattern = Pattern.compile("[0-9]*");
  return pattern.matcher(str).matches();
  }

  3.還是正則表達式public static boolean isNumeric(String str) {
  if (str.matches("\\d*")) {
  return true;
  }
  return false;
   }

  4.用ascii碼
  public static boolean isNumeric(String str) {
   for (int i = str.length(); --i >= 0;) {
  int chr = str.charAt(i);
   if (chr < 48 || chr > 57)
  return false;
  }
  return true;
  }

來源:http://www.linuxidc.com/Linux/2010-01/24244.htm



[火星人 ] JAVA判斷字元串是否為數字已經有275次圍觀

http://coctec.com/docs/program/show-post-71612.html