java判断字符串是否为数字,网上做法一般是把字符串中的字符一个个匹配,代码如下
- public static boolean isNumeric(String str){
- for (int i = 0; i < str.length(); i++){
- if (!Character.isDigit(str.charAt(i))){
- return false;
- }
- }
- return true;
- }
- 还有一种较为直接的办法:
- <pre class="java" name="code">public static boolean isNumeric(String str){
- try{
- int _int = Integer.parseInt(str);
- return true;
- }catch(Exception ex){
- return false;
- }
- }</pre><br>
- <pre></pre>