Method to determine whether a string is a number in java:
1. Use JAVA’s own function
1 2 3 4 5 6 7 8 9 |
|
charAt() method for Returns the character at the specified index. The index range is from 0 to length() - 1.
Syntax
1 |
|
Parameters: index -- The index of the character.
Return value: Returns the character at the specified index.
isDigit() method is used to determine whether the specified character is a number.
Syntax: public static boolean isDigit(char ch)
Parameters: ch -- The character to be tested.
Return value: If the character is a number, return true; otherwise, return false.
2. Use regular expressions
First import java.util.regex.Pattern and java.util.regex.Matcher
1 2 3 4 5 6 7 8 |
|
3. Use org .apache.commons.lang
1 2 |
|
For more java knowledge, please pay attention to the java basic tutorial column.
The above is the detailed content of Java method to determine whether string is a number. For more information, please follow other related articles on the PHP Chinese website!