实现一个函数用来判断字符串是否表示数值

题目描述

请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+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]+)?");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值