java推断字符串是不是为整数的要领【JAVA教程】,java

java推断字符串是不是为整数的要领:
要领一:用JAVA自带的函数
/** * * @Description: 用JAVA自带的函数 * * @param str * @return * @return: boolean * @Version: 0.0.1 */ public static boolean isOne(String str) { for (int i = str.length(); --i >= 0;) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; }
要领二:
/** * * @Description: 推断是不是为整数 * * @param str * @return * @return: boolean * @Version: 0.0.1 */ public static boolean isTwo(String str) { Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); return pattern.matcher(str).matches(); }
要领三:
/** * @Description: * @param str * @return * @return: boolean * @Version: 0.0.1 */ public static boolean isNumeric(String str) { Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); }
要领四:
/** * @Description: * @param s * @return * @return: boolean * @Version: 0.0.1 */ public final static boolean isNumeric(String s) { if (s != null && !"".equals(s.trim())) return s.matches("^[0-9]*$"); else return false; }
要领五:用ascii码
/** * @Description: * @param str * @return * @return: boolean * @Version: 0.0.1 */ 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; }
要领六:
/** * @Description: 推断double是不是是整数 * @param obj * @return * @return: boolean * @Version: 0.0.1 */ public static boolean isIntegerForDouble(double obj) { double eps = 1e-10; // 精度局限 return obj-Math.floor(obj) < eps; }
更多java学问请关注java基础教程栏目。
以上就是java推断字符串是不是为整数的要领的细致内容,更多请关注ki4网别的相干文章!