Java中判断,一个字符串是否全是数字
总代码:
九种方法,总有几个是看得懂的。
代码前三个用了Java自带的方法,后部分用的都是正则表达式。
package com.guo;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
System.out.println(isNum3("str324234"));
System.out.println(isNum3("ew2345errw"));
System.out.println(isNum3("43535334"));
System.out.println(isNum3("34"));
System.out.println(isNum3("3423_2313"));
System.out.println(isNum3("12水56"));
System.out.println(isNum3("-123456"));
System.out.println(isNum3("123456"));
}
public static boolean isNum(String str){
if (str.length() == 0 || str == null)
return false;
for (int i = 0; i < str.length(); i++){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
public static boolean isAllNumber(String str){
char[] data= str.toCharArray();
for (int i = 0; i <data.length ; i++) {
char c=data[i];
// 拆字符用的挺多,好记还灵活,就是麻烦
// 判断字符是否为纯数字和字母
// if (c<‘0’||c>‘9’ && c<‘A’||c>‘Z’ && c<‘a’||c>‘a’)
if(c<'0'|| c>'9'){
return false;
}
}
return true;
}
private static boolean isNumString(String str) {
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (!(ch >= '0' && ch <= '9')) {
return false;
}
}
return true;
}
// 以下是正则表达式部分
public static boolean isDigit1(String str) {
if (str == null)
return false;
return str.matches("[0-9]{1,}");
}
public static boolean isDigit2(String strNum) {
Pattern pattern = Pattern.compile("[0-9]{1,}");
Matcher matcher = pattern.matcher(strNum);
return matcher.matches();
}
public static boolean isNum1(String strNum) {
//Pattern pattern = Pattern.compile("[0-9]*"); // 此方法只验证正数
//Pattern pattern = Pattern.compile("-[0-9]*"); // 此方法只验证负数
Pattern pattern = Pattern.compile("-?[0-9]*"); // 验证数字
// Pattern pattern = Pattern.compile("-?[0-9]+?[0-9]+");(与上一行同效果)
Matcher matcher = pattern.matcher(strNum);
return matcher.matches();
}
public static boolean isNum2(String str){
return str.matches("[0-9]+");
}
public static boolean isNum3(String str) {
Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
return pattern.matcher(str).matches();
}
// 与isNum1效果相同
public static boolean isNum4(String str) {
Pattern pat = Pattern.compile("^-?[0-9]+$");
Matcher mat = pat.matcher(str);
if (mat.find()) {
return true;
} else {
return false;
}
}
}
运行结果:
false
false
true
true
false
false
true
true
Java自带的方法:
使用了Character(char的包装类)的方法
public static boolean isNum(String str){
if (str.length() == 0 || str == null)
return false;
for (int i = 0; i < str.length(); i++){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
将字符串拆成字符
// 将字符串一次性拆分为字符型数组,判断数组里的字符是不是数字
public static boolean isAllNumber(String str){
char[] data= str.toCharArray();
for (int i = 0; i <data.length ; i++) {
char c=data[i];
// 拆字符用的挺多,好记还灵活,就是麻烦
// 判断字符是否为纯数字和字母
// if (c<‘0’||c>‘9’ && c<‘A’||c>‘Z’ && c<‘a’||c>‘a’)
// if(chr<48 || chr>57) 跟下面一行同效果
if(c<'0'|| c>'9'){
return false;
}
}
return true;
}
// 将字符串逐个拆分,依次判断拆分而来的字符是不是数字
private static boolean isNumString(String str) {
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (!(ch >= '0' && ch <= '9')) {
return false;
}
}
return true;
}
正则表达式:
正则表达式不是很懂。。但它执行效率高。
public static boolean isDigit1(String str) {
if (str == null)
return false;
return str.matches("[0-9]{1,}");
}
public static boolean isDigit2(String strNum) {
Pattern pattern = Pattern.compile("[0-9]{1,}");
Matcher matcher = pattern.matcher(strNum);
return matcher.matches();
}
public static boolean isNum1(String strNum) {
//Pattern pattern = Pattern.compile("[0-9]*"); // 此方法只验证正数
//Pattern pattern = Pattern.compile("-[0-9]*"); // 此方法只验证负数
Pattern pattern = Pattern.compile("-?[0-9]*"); // 验证数字
// Pattern pattern = Pattern.compile("-?[0-9]+?[0-9]+");(与上一行同效果)
Matcher matcher = pattern.matcher(strNum);
return matcher.matches();
}
public static boolean isNum2(String str){
return str.matches("[0-9]+");
}
public static boolean isNum3(String str) {
Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
return pattern.matcher(str).matches();
}
// 与isNum1效果相同
public static boolean isNum4(String str) {
Pattern pat = Pattern.compile("^-?[0-9]+$");
Matcher mat = pat.matcher(str);
if (mat.find()) {
return true;
} else {
return false;
}
}