码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-09-09 16:42:32      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

判断字符串是不是数字?

方法一:

/**
* 用于验证获取的字符串是不是数字
* @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~

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

标签:

原文地址:http://www.cnblogs.com/yoghurt/p/5857034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!