Home  >  Article  >  Java  >  Java determines whether a string contains numbers

Java determines whether a string contains numbers

尚
Original
2019-11-21 11:04:575098browse

Java determines whether a string contains numbers

In java, you can use the java.lang.character.isDigit(); method to determine whether a character is a number. In java, you can traverse a string and use java.lang.character. isDigit(); method determines whether there are numbers in it.

Declaration of java.lang.Character.isDigit() method

public static boolean isDigit(char ch)

Parameters: ch - the character to be tested

Return value: If the character is a number, this method returns true, otherwise false is returned.

Example:

public static void main(String[] args){
boolean isDigit = false;//定义一个boolean值,用来表示是否包含数字
boolean isLetter = false;//定义一个boolean值,用来表示是否包含字母
String str = "aaasss8fff"; //假设有一个字符串
for(int i=0 ; i<str.length() p="" 循环遍历字符串
if(Character.isDigit(str.charAt(i))){ //用char包装类中的判断数字的方法判断每一个字符
isDigit = true;
}
if(Character.isLetter(str.charAt(i))){ //用char包装类中的判断字母的方法判断每一个字符
isLetter = true;
}
}

For more java knowledge, please pay attention to java basic tutorial.

The above is the detailed content of Java determines whether a string contains numbers. 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
Previous article:What is a lock in javaNext article:What is a lock in java