您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java中判断字符是否为数字

2024/5/12 8:48:51发布33次查看
一、使用自带函数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中判断字符是否为数字的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product