Home > Java > Java Basics > body text

How to determine whether a string consists of characters in java

王林
Release: 2019-11-28 09:44:41
Original
2306 people have browsed it

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]*");
}
Copy after login

Extension:

Determine whether they are all numbers:

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

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!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!