Home  >  Article  >  Java  >  How to tell if a string contains only numbers and letters in java?

How to tell if a string contains only numbers and letters in java?

青灯夜游
青灯夜游Original
2019-12-30 17:51:2611066browse

How to tell if a string contains only numbers and letters in java?

#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".

Recommended learning:

Java video tutorial

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn