表示數值的字符串

使用正則表達式進行匹配。java

[]  : 字符集合
()  : 分組
?   : 重複 0 ~ 1 次
+   : 重複 1 ~ n 次
*   : 重複 0 ~ n 次
.   : 任意字符
\\. : 轉義後的 .
\\d : 數字
public boolean isNumeric(char[] str) {
    if (str == null || str.length == 0)
        return false;
    return new String(str).matches("[+-]?\\d*(\\.\\d+)?([eE][+-]?\\d+)?");
}
相關文章
相關標籤/搜索