---
title: "java中判断字符串是否为数字的三种方法"
author: "congjl2002"
date: "2010-09-27T11:06:37Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2010-09-27T11:06:37Z"
id: 772952
url: "https://congjl2002.iteye.com/blog/772952"
---

1用JAVA自带的函数  
public   static   boolean  isNumeric(String str){  
   for  ( int  i  =   str.length(); --i&gt;=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 str.matches("\\\\d\*");  
    return pattern.matcher(str).matches();     
 }

3用ascii码

public static boolean isNumeric(String str){  
   for (int i=str.length();--i&gt;=0;){  
      int chr=str.charAt(i);  
      if(chr&lt;48 || chr&gt;57)  
         return false ;  
   }  
   return true ;  
}