/**
* @author lautturi.com
* Java example: check if a string can be parsed to a number in java
*/
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Lautturi {
public static void main(String[] args) {
String str1 = "123456";
String regex = "[0-9]+"; // "\\d+"
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str1);
Boolean b = matcher.matches();
System.out.println(b);
String str2 = "123abc";
Matcher matcher2 = pattern.matcher(str2);
System.out.println(matcher2.matches());
}
}Source:wwal.wutturi.com/**
* @author lautturi.com
* Java example: Tells whether or not the string is a number in java
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
String str1 = "123456";
String regex = "[0-9]+"; // "\\d+"
Boolean b = str1.matches(regex);
System.out.println(b);
String str2 = "123abc";
System.out.println(str2.matches(regex));
}
}
output:
true false