题目描述
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100",“5e2”,"-123",“3.1416"和”-1E-16"都表示数值。 但是"12e",“1a3.14”,“1.2.3”,"±5"和"12e+4.3"都不是
实现思路
1、将字符串看作A.BeC/A.BEC模型,其中A为整数,B为小数部分,C为整数(方法比较繁琐)
代码
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public boolean isNumeric(char[] str) {
String str1 = String.valueOf(str);
String strA = null,strB = null,strC = null;
String str3 = "";
if (str1.contains("\\.") && (str1.contains("e") || str1.contains("E"))){
if (str1.contains("e")){
if (str1.indexOf("\\.") < str1.indexOf("e")){
return false;
}
}
if (str1.contains("E")){
if (str1.indexOf("\\.") < str1.indexOf("E")){
return false;
}
}
}
if (str1.contains("\\.") || str1.contains("e") || str1.contains("E")) {
if (str1.contains(".")) {//判断是否包含小数点
//若包含,则分割开
String[] str2 = str1.split("\\.");
//判断 小数点个数是否为1个
if (str2.length != 2) {
return false;
}
strA = str2[0];
str3 = str2[1];
if (!isNumeric(strA)){
return false;
}
if (str3.contains("e") && !str3.contains("E")) {
String[] str4 = str3.split("e");
if (str4.length != 2) {
return false;
}
strB = str4[0];
strC = str4[1];
if (!isNumeric(strB)){
return false;
}
if (!isNumeric(strC)){
return false;
}
} else if (!str3.contains("e") && str3.contains("E")) {
String[] str5 = str3.split("E");
if (str5.length != 2) {
return false;
}
strB = str5[0];
strC = str5[1];
if (!isNumeric(strB)){
return false;
}
if (!isNumeric(strC)){
return false;
}
} else if (str3.contains("e") && str3.contains("E")) {
return false;
} else {
strB = str3;
}
}else{
if (str1.contains("e") && !str1.contains("E")) {
String[] str4 = str1.split("e");
if (str4.length != 2) {
return false;
}
strA = str4[0];
strC = str4[1];
if (!isNumeric(strA)){
return false;
}
if (!isNumeric(strC)){
return false;
}
} else if (!str1.contains("e") && str1.contains("E")) {
String[] str5 = str1.split("E");
if (str5.length != 2) {
return false;
}
strA = str5[0];
strC = str5[1];
if (!isNumeric(strA)){
return false;
}
if (!isNumeric(strC)){
return false;
}
} else if (str1.contains("e") && str1.contains("E")) {
return false;
}
}
}else {
if (str1.contains("\\+") && str1.contains("\\-")){
return false;
}
if (!isNumeric(str1)){
return false;
}
return true;
}
if(strA !=null && strB != null && strC != null){
if ((strA .contains("\\-") && strA.contains("\\+")) || (strB.contains("\\-") && strB.contains("\\+")) || (strC.contains("-")
&& strC.contains("\\+"))){
return false;
}
}else if (strA !=null && strB != null){
if ((strA .contains("\\-") && strA.contains("\\+")) || (strB.contains("\\-") && strB.contains("\\+"))){
return false;
}
}else if (strA !=null && strC != null){
if ((strA .contains("\\-") && strA.contains("\\+")) || (strC.contains("\\-") && strC.contains("\\+"))){
return false;
}
}
return true;
}
/**
* 用以确定是否为纯数字,若是返回true,否则返回false;
*/
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");
String bigStr;
try {
bigStr = new BigDecimal(str).toString();
} catch (Exception e) {
return false;//异常 说明包含非数字。
}
Matcher isNum = pattern.matcher(bigStr); // matcher是全匹配
if (!isNum.matches()) {
return false;
}
return true;
}
}
2、使用正则表达式:(借鉴)
正则表达式语法规则:可参考如下链接
正则表达式语法
public class Solution {
public boolean isNumeric(char[] str) {
String s=String.valueOf(str);
return s.matches("[+-]?[0-9]*(\\.[0-9]*)?([eE][+-]?[0-9]+)?");
}
}