Home  >  Article  >  Java  >  How to determine whether a string consists of characters in java

How to determine whether a string consists of characters in java

王林
王林Original
2019-11-28 09:44:412446browse

How to determine whether a string consists of characters in java

Judgment ideas:

First define a regular expression for judgment, for example: "^[a-zA-Z]* ", and then call the "matches" method to match the string that needs to be judged with the defined regular expression.

Free video tutorial recommendation: java learning

Judge whether all English characters:

public boolean isString(String s) {
return s.matches("^[a-zA-Z]*");
}

Extension:

Determine whether they are all numbers:

public boolean isNumber(String s) {
Pattern pattern = Pattern.compile("^[0-9]*");
return pattern.matcher(s).matches();
}

For more related articles and tutorials, you can visit: Getting Started with Java

The above is the detailed content of How to determine whether a string consists of characters 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