Validate if a given string is numeric.
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
原题链接:https://oj.leetcode.com/problems/valid-number/
判断字符串是否是数字。
规则:出现+, - 则必须是第一个,或前一个是e;有. 则是小数,之前不可有.和e;有e,则前面要有.,不能有e,并且后面要有.。
可用正则表达式来解答。
public static boolean isNumber(String s) {
String reg = "[+-]?(\\d+\\.?|\\.\\d+)\\d*(e[+-]?\\d+)?";
return s.trim().matches(reg);
}
本文介绍了一种使用正则表达式的方法来判断给定的字符串是否表示一个有效的数字。其中包括整数、小数、科学计数法等多种形式的数字,并详细解释了正则表达式的构成。
956

被折叠的 条评论
为什么被折叠?



