Java determines the number in a string: whether it is a number, whether it contains numbers, how to intercept numbers

Source: Internet
Author: User

Off Topic:

JavaScript determines whether a character is a number, using the function: IsDigit ();

First, determine whether a string is a number

Package com.cmc.util;import Java.util.regex.matcher;import java.util.regex.Pattern; Public classDigitutil { Public Static voidMain (string[] args) {String str="123d"; System. out. println (IsDigit2 (str)); }        //determines whether a character is a number     Public Staticboolean isdigit (String strnum) {returnStrnum.matches ("[0-9]{1,}"); }        //determines whether a string is numeric     Public Staticboolean isDigit2 (String strnum) {pattern pattern= Pattern.compile ("[0-9]{1,}"); Matcher Matcher=Pattern.matcher ((charsequence) strnum); returnmatcher.matches (); }}

Second, determine whether the string contains a number

// determines whether a string contains a number     Public boolean hasdigit (String content) {        false;         = Pattern.compile (". *\\d+.*");         = p.matcher (content);         if (M.matches ()) {            true;        }         return flag;    }

Third, intercept the numbers in the string

Package com.cmc.util;import Java.util.regex.matcher;import java.util.regex.Pattern; Public classDigitutil { Public Static voidMain (string[] args) {String str="123dsd99"; System. out. println (Getnumbers (str)); ===> 123
SYSTEM.OUT.PRINTLN (Splitnotnumber (str)); ===> DSD}//intercept Number "reads the first contiguous string in a string, and does not contain subsequent discontinuous numbers" Public Staticstring Getnumbers (string content) {pattern pattern= Pattern.compile ("\\d+"); Matcher Matcher=pattern.matcher (content); while(Matcher.find ()) {returnMatcher.group (0); } return ""; } //Intercept non-digital Public Staticstring Splitnotnumber (string content) {pattern pattern= Pattern.compile ("\\d+"); Matcher Matcher=pattern.matcher (content); while(Matcher.find ()) {returnMatcher.group (0); } return ""; }}

Iv. method of judging whether a string is a number

(i) using the Java Self-band function

     Public Static voidMain (string[] args) {String str="12399"; System. out. println (IsNumeric (str)); }        //determines whether a string is a number     Public Staticboolean isnumeric (String str) { for(inti = Str.length (); --i>=0;) {            if(!Character.isdigit (Str.charat (i))) {                return false; }        }        return true; }

(ii) using regular expressions

  public  static  void   main (string[] args) {String str  ="  123p99   "        ; System.  out     .println (IsNumeric2 (str));  public  static   Boolean isNumeric2 (String str) {pattern pattern  =pattern.compile (" [0-9]*   "         return   Pattern.matcher (str). matches (); }

(iii) using ASCII code

     Public Static voidMain (string[] args) {String str="12399"; System. out. println (ISNUMERIC3 (str)); }        //determines whether a string is a number     Public Staticboolean isNumeric3 (String str) { for(intI=str.length (); --i>=0;){            CharC=Str.charat (i); if(C < -|| C > $)                return false; }        return true; }

Java determines the number in a string: whether it is a number, whether it contains numbers, how to intercept numbers

Related Article

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.