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