Home  >  Article  >  Java  >  Java determines whether a string is an integer number

Java determines whether a string is an integer number

王林
王林Original
2020-01-16 09:34:063497browse

Java determines whether a string is an integer number

Code example:

/**
     * 判断字符串是否为数字
     */
    public static boolean isNumeric(String str){ 
        Pattern pattern = Pattern.compile("[0-9]*"); 
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();  
     }

(Free video tutorial recommendation: java video tutorial)

Test:

public static void main(String[] args) {
		String str = "11";
		String str1 = "11sdf";
		System.out.println(isNumeric(str));
		System.out.println(isNumeric(str1));
	}

Output results:

true
false

Recommended related articles and tutorials: java introductory tutorial

The above is the detailed content of Java determines whether a string is an integer number. 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