Find if the String is a number or floating point number - Regular Expression
Find if a string contains only digits?
Find if the String is a Floating Point number.
The caveat here is the strings are long text and Double.parseDouble() will be out of range. Such problems can be solved only using Regular Expressions.
1 2 3 4 5 | public static boolean stringContainsOnlyDigits(String s) { Pattern p = Pattern.compile( "^[0-9]+$" ); Matcher m = p.matcher( s ); return m.find(); } |
1 2 3 4 5 6 7 8 9 | public static boolean stringIsFloatingPointNumber(String s) { Pattern p = Pattern.compile( "^[-+]?[0-9]*\\.?[0-9]+$" ); Matcher m = p.matcher( s ); boolean found = m.find(); if (found) { System.out.println( "found: " + m.group( 0 )); } return found; } |
The caveat here is the strings are long text and Double.parseDouble() will be out of range. Such problems can be solved only using Regular Expressions.
Comments
Post a Comment