您好,欢迎访问一九零五行业门户网

Java中判断字符是否为数字

一、使用自带函数isdigit()
语法:
public static boolean isdigit(char ch)
实例:
public static boolean isnumeric (string str) { for (int i = str.length(); --i >=0) { if (!character.isdigit(str.charat(i))) { return false; } } return true;}
二、使用正则表达式
实例1:
public static boolean isnumeric(string str) { pattern pattern = pattern.compile("^[-\\+]?[\\d]*$"); return pattern.matcher(str).matches();}
实例2:
public static boolean isnumeric(string str) { if (str != null && !"".equals(str.trim())) { return s.matches("^[0-9]*$"); } return false;}
实例3:
public static boolean isnumeric (string str) { pattern pattern = pattern.compile("[0-9]*"); return pattern.matcher(str).matcher();}
三、使用ascii码
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;}
推荐教程:java开发入门
以上就是java中判断字符是否为数字的详细内容。
其它类似信息

推荐信息