判断字符串是不是数字的方法

判断字符串是不是数字?

方法一:

/**
* 用于验证获取的字符串是不是数字
* @param str
* @return
*/
public static boolean isNumeric(String str) {
  for (int i = 0; i < str.length(); i++) {
  // 验证字符串中的字符是不是数字
    if (!Character.isDigit(str.charAt(i))) {
      return false;
    }
  }
  return true;
}

 

方法二:

// 判断月份是否为空或者是否为非数字,正则表达式
public static boolean isVaild(String s) {
  Pattern pattern = Pattern.compile("[0-9]*");
  Matcher isNum = pattern.matcher(s);

  if (s != null && isNum.matches()) {
    return true;
  }
  return false;
}

 

拿走,不谢~O(∩_∩)O~

posted @ 2016-09-09 16:06  yoghurt2016  阅读(879)  评论(0编辑  收藏  举报