📄 utilvalidate.java
字号:
return Character.isDigit(c); } /** Returns true if character c is a letter or digit. */ public static boolean isLetterOrDigit(char c) { return Character.isLetterOrDigit(c); } /** Returns true if all characters in string s are numbers. * * Accepts non-signed integers only. Does not accept floating * point, exponential notation, etc. */ public static boolean isInteger(String s) { if (isEmpty(s)) return defaultEmptyOK; // Search through string's characters one by one // until we find a non-numeric character. // When we do, return false; if we don't, return true. for (int i = 0; i < s.length(); i++) { // Check that current character is number. char c = s.charAt(i); if (!isDigit(c)) return false; } // All characters are numbers. return true; } /** Returns true if all characters are numbers; * first character is allowed to be + or - as well. * * Does not accept floating point, exponential notation, etc. */ public static boolean isSignedInteger(String s) { if (isEmpty(s)) return defaultEmptyOK; try { Integer.parseInt(s); return true; } catch (Exception e) { return false; } // int startPos = 0; // boolean secondArg = defaultEmptyOK; // if(isSignedInteger.arguments.length > 1) secondArg = isSignedInteger.arguments[1]; // skip leading + or - // if((s.charAt(0) == "-") ||(s.charAt(0) == "+") ) startPos = 1; // return(isInteger(s.substring(startPos, s.length), secondArg)) } /** Returns true if all characters are numbers; * first character is allowed to be + or - as well. * * Does not accept floating point, exponential notation, etc. */ public static boolean isSignedLong(String s) { if (isEmpty(s)) return defaultEmptyOK; try { Long.parseLong(s); return true; } catch (Exception e) { return false; } } /** Returns true if string s is an integer > 0. NOTE: using the Java Long object for greatest precision */ public static boolean isPositiveInteger(String s) { if (isEmpty(s)) return defaultEmptyOK; try { long temp = Long.parseLong(s); if (temp > 0) return true; return false; } catch (Exception e) { return false; } // return(isSignedInteger(s, secondArg) // &&((isEmpty(s) && secondArg) ||(parseInt(s) > 0) ) ); } /** Returns true if string s is an integer >= 0. */ public static boolean isNonnegativeInteger(String s) { if (isEmpty(s)) return defaultEmptyOK; try { int temp = Integer.parseInt(s); if (temp >= 0) return true; return false; } catch (Exception e) { return false; } // return(isSignedInteger(s, secondArg) // &&((isEmpty(s) && secondArg) ||(parseInt(s) >= 0) ) ); } /** Returns true if string s is an integer < 0. */ public static boolean isNegativeInteger(String s) { if (isEmpty(s)) return defaultEmptyOK; try { int temp = Integer.parseInt(s); if (temp < 0) return true; return false; } catch (Exception e) { return false; } // return(isSignedInteger(s, secondArg) // &&((isEmpty(s) && secondArg) ||(parseInt(s) < 0) ) ); } /** Returns true if string s is an integer <= 0. */ public static boolean isNonpositiveInteger(String s) { if (isEmpty(s)) return defaultEmptyOK; try { int temp = Integer.parseInt(s); if (temp <= 0) return true; return false; } catch (Exception e) { return false; } // return(isSignedInteger(s, secondArg) // &&((isEmpty(s) && secondArg) ||(parseInt(s) <= 0) ) ); } /** True if string s is an unsigned floating point(real) number. * * Also returns true for unsigned integers. If you wish * to distinguish between integers and floating point numbers, * first call isInteger, then call isFloat. * * Does not accept exponential notation. */ public static boolean isFloat(String s) { if (isEmpty(s)) return defaultEmptyOK; boolean seenDecimalPoint = false; if (s.startsWith(decimalPointDelimiter)) return false; // Search through string's characters one by one // until we find a non-numeric character. // When we do, return false; if we don't, return true. for (int i = 0; i < s.length(); i++) { // Check that current character is number. char c = s.charAt(i); if (c == decimalPointDelimiter.charAt(0)) { if (!seenDecimalPoint) { seenDecimalPoint = true; } else { return false; } } else { if (!isDigit(c)) return false; } } // All characters are numbers. return true; } /** General routine for testing whether a string is a float. */ public static boolean isFloat(String s, boolean allowNegative, boolean allowPositive, int minDecimal, int maxDecimal) { if (isEmpty(s)) return defaultEmptyOK; try { float temp = Float.parseFloat(s); if (!allowNegative && temp < 0) return false; if (!allowPositive && temp > 0) return false; String floatString = Float.toString(temp); int decimalPoint = floatString.indexOf("."); if (decimalPoint == -1) { if (minDecimal > 0) return false; return true; } // 1.2345; length=6; point=1; num=4 int numDecimals = floatString.length() - decimalPoint; if (minDecimal >= 0 && numDecimals < minDecimal) return false; if (maxDecimal >= 0 && numDecimals > maxDecimal) return false; return true; } catch (Exception e) { return false; } } /** General routine for testing whether a string is a double. */ public static boolean isDouble(String s, boolean allowNegative, boolean allowPositive, int minDecimal, int maxDecimal) { if (isEmpty(s)) return defaultEmptyOK; try { double temp = Double.parseDouble(s); if (!allowNegative && temp < 0) return false; if (!allowPositive && temp > 0) return false; String doubleString = Double.toString(temp); int decimalPoint = doubleString.indexOf("."); if (decimalPoint == -1) { if (minDecimal > 0) return false; return true; } // 1.2345; length=6; point=1; num=4 int numDecimals = doubleString.length() - decimalPoint; if (minDecimal >= 0 && numDecimals < minDecimal) return false; if (maxDecimal >= 0 && numDecimals > maxDecimal) return false; return true; } catch (Exception e) { return false; } } /** True if string s is a signed or unsigned floating point * (real) number. First character is allowed to be + or -. * * Also returns true for unsigned integers. If you wish * to distinguish between integers and floating point numbers, * first call isSignedInteger, then call isSignedFloat. */ public static boolean isSignedFloat(String s) { if (isEmpty(s)) return defaultEmptyOK; try { Float.parseFloat(s); return true; } catch (Exception e) { return false; } //The old way: // int startPos = 0; // if(isSignedFloat.arguments.length > 1) secondArg = isSignedFloat.arguments[1]; // skip leading + or - // if((s.charAt(0) == "-") ||(s.charAt(0) == "+") ) startPos = 1; // return(isFloat(s.substring(startPos, s.length), secondArg)) } /** True if string s is a signed or unsigned floating point * (real) number. First character is allowed to be + or -. * * Also returns true for unsigned integers. If you wish * to distinguish between integers and floating point numbers, * first call isSignedInteger, then call isSignedFloat. */ public static boolean isSignedDouble(String s) { if (isEmpty(s)) return defaultEmptyOK; try { Double.parseDouble(s); return true; } catch (Exception e) { return false; } } /** Returns true if string s is letters only. * * NOTE: This should handle i18n version to support European characters, etc. * since it now uses Character.isLetter() */ public static boolean isAlphabetic(String s) { if (isEmpty(s)) return defaultEmptyOK; // Search through string's characters one by one // until we find a non-alphabetic character. // When we do, return false; if we don't, return true. for (int i = 0; i < s.length(); i++) { // Check that current character is letter. char c = s.charAt(i); if (!isLetter(c)) return false; } // All characters are letters. return true; } /** Returns true if string s is English letters (A .. Z, a..z) and numbers only. * * NOTE: Need i18n version to support European characters. * This could be tricky due to different character * sets and orderings for various languages and platforms. */ public static boolean isAlphanumeric(String s) { if (isEmpty(s)) return defaultEmptyOK; // Search through string's characters one by one // until we find a non-alphanumeric character. // When we do, return false; if we don't, return true. for (int i = 0; i < s.length(); i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -