Valid Number,判断是否为合法数字
2016-08-16 16:09
302 查看
问题描述:
Validate if a given string is numeric.
Some examples:
Validate if a given string is numeric.
Some examples:
"0"=>
true
" 0.1 "=>
true
"abc"=>
false
"1 a"=>
false
"2e10"=>
true
public boolean isNumber(String s) { s = s.trim(); if (s.length() == 0) return false; boolean hasE = false; boolean hasDot = false; boolean hasNumber = false; for (int i = 0; i < s.length(); i++) { // e cannot be the first character if (i == 0 && s.charAt(i) == 'e') return false; if (s.charAt(i) == 'e') { // e cannot be replicated nor placed before number if (hasE == true || hasNumber == false) { return false; } else { hasE = true; } } if (s.charAt(i) == '.') { // '.' cannot be replicated nor placed after 'e' if (hasDot == true || hasE == true) { return false; } else { hasDot = true; } } // the sign can be placed at the beginning or after 'e' if (i != 0 && s.charAt(i - 1) != 'e' && (s.charAt(i) == '+' || s.charAt(i) == '-')) return false; // no other chacraters except '+', '-', '.', and 'e' if ((s.charAt(i) > '9' || s.charAt(i) < '0') && s.charAt(i) != '+' && s.charAt(i) != '-' && s.charAt(i) != '.' && s.charAt(i) != 'e') return false; // check whether numbers are included. if (s.charAt(i) <= '9' && s.charAt(i) >= '0') { hasNumber = true; } } // '+', '-', and 'e' cannot be the last character if (s.charAt(s.length() - 1) == '-' || s.charAt(s.length() - 1) == '+' || s.charAt(s.length() - 1) == 'e') return false; return hasNumber; } }
相关文章推荐
- C# 判断一字符串是否为合法数字(正则表达式)
- 判断IOS 数字是否合法,邮箱是否合法
- 合法的C标示符(判断是否是数字或字母)
- valid number 判断字符串是否为有效数字
- Valid Number 判断一个数字是否有效
- C# 判断一字符串是否为合法数字(正则表达式)
- LeetCode65——Valid Number(使用DFA)来判断字符串是否为数字
- CnCrypt代码 之 判断字符串是否是合法的数字字符串
- VC判断字符串是否为合法数字
- LeetCode Valid Number(判断字符串是否是合法的数字表示 )
- 用正则表达式判断输入的数字是否合法的例子
- 校验出生日期,校验身份证格式,校验会员账户:9开头数字,校验手机号码是否合法,判断字符串中的url是否都是图片格式字符串,通过图片url读取图片并将图片转换成base64编码
- C# 判断一字符串是否为合法数字(正则表达式)
- 判断一个字符串是否是合法数字串
- [LeetCode]—Valid Number 判断字符串是否为合法数据(科学计数法)
- 输入一个卡号判断是否合法。其中卡号长度为16-19位,只能是数字。满足:
- Java判断数字是否为整数
- oracle 判断字段是否为是数字 regexp_like用法 正则表达式
- 数据结构实验题--判断入栈出栈操作序列是否合法
- JavaScript判断数字是否为质数的方法汇总