Five ways to judge whether a string is a number in Java

Source: Internet
Author: User

It is recommended to use the second method, the fastest one.

Method One: Use Java's own function

Java code
    1. Public Static boolean isnumeric (String str) {
    2. For (int i = Str.length ();--i>=0;) {
    3. if (! Character.isdigit (Str.charat (i))) {
    4. return false;
    5. }
    6. }
    7. return true;
    8. }
public static Boolean isnumeric (String str) {for  (int i = Str.length ();--i>=0;) {     if (! Character.isdigit (Str.charat (i))) {    return false;   }  }  return true;}



Method Two:

Java code
    1. /*  
    2.   *  Determines whether it is an integer    
    3.   *  @param  str  incoming string    
    4.   *  @return   is an integer returns True, otherwise returns false   
    5. */  
    6.   
    7.   
    8.   public static boolean  isinteger (STRING STR)  {     
    9.      pattern pattern = pattern.compile ( "^[-\\+]?[ \\d]*$ ");     
    10.     return  Pattern.matcher (str). Matches ();     
    11.   }  
  /* * Determine if integer   * @param str passed in string   * @return is integer returns TRUE, otherwise false *  /public static boolean Isinteger (string STR) {      Pattern pattern = pattern.compile ("^[-\\+]?[ \\d]*$ ");      return Pattern.matcher (str). matches ();    }



Method Three:

Java code
    1. Public Static boolean isnumeric (String str) {
    2. Pattern pattern = Pattern.compile ("[0-9]*");
    3. return Pattern.matcher (str). matches ();
    4. }
public static Boolean isnumeric (String str) {    Pattern pattern = Pattern.compile ("[0-9]*");    return Pattern.matcher (str). matches ();   }



Method Four:

Java code
    1. public final  Static boolean isnumeric (string s)  {  
    2.         if  (s ! = null && !
    3.              return s.matches (
    4.         else   
    5.              return false;   
    6.     }  
Public final Static Boolean isnumeric (String s) {if (s! = null &&! "). Equals (S.trim ())) return s.matches ("^[0-9]*$"); Elsereturn false;}



3 method Five: using ASCII code

Java code
  1. Public Static boolean isnumeric (String str) {
  2. For (int i=str.length ();--i>=0;) {
  3. int Chr=str.charat (i);
  4. if (chr<| | chr>)
  5. return false;
  6. }
  7. return true;
  8. }
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;   }   



Article Address:
http://javapub.iteye.com/blog/666544

Five ways to judge whether a string is a number in Java (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.