http://cherryqq.iteye.com/blog/406355
In JavaScript, there is a method isdigit () used to determine whether a string is a number, in the Java string processing method does not have such a method, it is often necessary to use, so the Internet search, sorted out two with regular expression matching method of judgment, as follows;
Java code
- Determines whether a string is numeric
- Public Boolean isdigit (String strnum) {
- return Strnum.matches ("[0-9]{1,}");
- }
- Determines whether a string is numeric
- Public Boolean isdigit (String strnum) {
- Pattern pattern = Pattern.compile ("[0-9]{1,}");
- Matcher Matcher = Pattern.matcher ((charsequence) strnum);
- return matcher.matches ();
- }
- Intercept numbers
- public string Getnumbers (string content) {
- Pattern pattern = Pattern.compile ("\\d+");
- Matcher Matcher = pattern.matcher (content);
- While (Matcher.find ()) {
- return Matcher.group (0);
- }
- return "";
- }
- Intercept non-digital
- public string Splitnotnumber (string content) {
- Pattern pattern = Pattern.compile ("\\d+");
- Matcher Matcher = pattern.matcher (content);
- While (Matcher.find ()) {
- return Matcher.group (0);
- }
- return "";
- }
- Determines whether a string contains a number
public boolean hasdigit (String content) {
Boolean flag = false;
Pattern p = pattern.compile (". *\\d+.*");
Matcher m = p.matcher (content);
if (M.matches ())
Flag = true;
return flag;
}
Three ways to judge whether a string is a number in Java
1 functions in Java
public static Boolean isnumeric (String str) {
for (int i = Str.length ();--i>=0;) {
if (! Character.isdigit (Str.charat (i))) {
return false;
}
}
return true;
}
2 Using regular expressions
public static Boolean isnumeric (String str) {
Pattern pattern = Pattern.compile ("[0-9]*");
return Pattern.matcher (str). matches ();
}
3 with ASCII code
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;
}
return true;
}
In Java, determine whether a string is "both numeric" and "contains numbers" and "intercepts numbers" (GO)