Home >Java >JavaBase >How to determine whether a string is a decimal in java

How to determine whether a string is a decimal in java

王林
王林Original
2019-11-26 17:42:584686browse

How to determine whether a string is a decimal in java

函数介绍:

matches() 方法用于检测字符串是否匹配给定的正则表达式。

语法:

public boolean matches(String regex)

返回值:

在字符串匹配给定的正则表达式时,返回 true。

StringUtils.isBlank(String str)判断某字符串是否为空或长度为0或由空白符(whitespace) 构成。

免费相关学习视频推荐:java学习视频

示例如下:

 /**
     * 判断是否是整数或者是小数 
     * @param str
     * @return true:是,false不是
     */
    private boolean validateNumber(String str) {
        if(StringUtils.isBlank(str)) {
            return false;
        }
        // 说明一下的是该正则只能识别4位小数;如果不限制小数位数的话,写成[+-]?[0-9]+(\\.[0-9]+)?就可以了
        return str.matches("[+-]?[0-9]+(\\.[0-9]{1,4})?");    
    }

推荐相关文章教程:java零基础入门

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