博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java判断字符串是否为数字的自定义方法
阅读量:4538 次
发布时间:2019-06-08

本文共 1069 字,大约阅读时间需要 3 分钟。

//方法一:用JAVA自带的函数public static boolean isNumeric(String str){   for (int i = str.length();--i>=0;){         if (!Character.isDigit(str.charAt(i))){           return false;       }   }   return true;}
/*方法二:推荐,速度最快 (有争议)  * 判断是否为整数   * @param str 传入的字符串   * @return 是整数返回true,否则返回false */  public static boolean isInteger(String str) {          Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");          return pattern.matcher(str).matches();    }
//方法三:public static boolean isNumeric(String str){    Pattern pattern = Pattern.compile("[0-9]*");    return pattern.matcher(str).matches();   }
//方法四:public final static boolean isNumeric(String s) {    if (s != null && !"".equals(s.trim()))        return s.matches("^[0-9]*$");    else        return false;}
//方法五:用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;}

 

转自: https://www.cnblogs.com/heyuxiu/p/5972187.html [非常感谢: )   ]

转载于:https://www.cnblogs.com/cjdty/p/9021475.html

你可能感兴趣的文章
[总结]数据结构(板子)
查看>>
网页图片加载失败,用默认图片替换
查看>>
C# 笔记
查看>>
2013年10月13日学习:SQL通过命令语句来创建表
查看>>
剑指offer : 二维数组中的查找
查看>>
第三章 python基础
查看>>
java基础题
查看>>
[转]人人店短信插件开发
查看>>
[转]c# System.IO.Ports SerialPort Class
查看>>
14. 最长公共前缀
查看>>
Redis文档
查看>>
项目重构
查看>>
(笔试题)和一半的组合数
查看>>
leetcode--Algorithm--Array_Part 1 Easy- 566 Reshape the Matrix
查看>>
AC自动机算法详解 (转载)
查看>>
python3-day5(模块)
查看>>
Linux配置JDK
查看>>
qt 读取xml文件
查看>>
python3之正则表达式
查看>>
Visual Studio提示“无法启动IIS Express Web服务器”的解决方法
查看>>