您的位置:首页 > 编程语言 > Java开发

JAVA判断字符串是否为数字

2010-07-15 13:48 295 查看
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://java.chinaitlab.com/base/801693.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: