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

Java determines whether a string is an integer number

王林
Release: 2020-01-16 09:34:06
Original
3536 people have browsed it

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();  
     }
Copy after login

(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));
	}
Copy after login

Output results:

true
false
Copy after login

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!

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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template