#How does Java determine if a string contains only numbers and letters?
In java, you can use regular expressions ("^[a-z0-9A-Z] $") to determine whether a string contains only numbers and letters.
Go directly to the code
public static boolean isLetterDigit(String str) { String regex = "^[a-z0-9A-Z]+$"; return str.matches(regex); }
java regular expression
Regular expression defines the pattern of the string.
Regular expressions can be used to search, edit or process text.
Regular expressions are not limited to a certain language, but there are subtle differences in each language.
The following table lists some examples and descriptions of regular expressions:
Regular expressions | Description |
---|---|
this is text |
Matches the string "this is text" |
this\s is\s text |
Note the \s in the string. Match the \s after the word "this" to match multiple spaces, then match the is string, and then \s match multiple spaces and then follow on text string. can match this instance: this is text |
##^\d (\.\d )? | ^ Defines what starts with \d Matches one or more digits? Setting options within brackets is optional\. Matches " ." Matches examples: "5", "1.5" and "2.21". |
The above is the detailed content of How to tell if a string contains only numbers and letters in java?. For more information, please follow other related articles on the PHP Chinese website!