字符串:表示数值的字符串

题目描述
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。

方法一:

public class Solution {
    public boolean isNumeric(char[] str) {
        try {                  
            Double.parseDouble(new String(str)); //String.valueOf(str)
            return true;
        } catch(Exception e) {
            return false;
        }
    }
}

方法二:

public class Solution {
    public boolean isNumeric(char[] str) {
        return isNumeric(new String(str));
    }

     /**
     * 题目:请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。
     *
     * @param string
     * @return
     */
    public static boolean isNumeric(String string) {
        if (string == null || string.length() < 1) {
            return false;
        }
        int index = 0;

        if (string.charAt(index) == '+' || string.charAt(index) == '-') {
            index++;
        }

        // 已经到达字符串的末尾了
        if (index >= string.length()) {
            return false;
        }

        boolean numeric = true;
        index = scanDigits(string, index);
        // 还未到字符串的末尾
        if (index < string.length()) {
            // 如果是小数点
            if (string.charAt(index) == '.') {
                // 移动到下一个位置
                index++;
                index = scanDigits(string, index);

                // 已经到了字符串的末尾了
                if (index >= string.length()) {
                    numeric = true;
                }
                // 还未到字符串结束位置
                else if (index < string.length() && (string.charAt(index) == 'e' || string.charAt(index) == 'E')) {
                    numeric = isExponential(string, index);
                } else {
                    numeric = false;
                }
            }
            // 如果是指数标识
            else if (string.charAt(index) == 'e' || string.charAt(index) == 'E') {
                numeric = isExponential(string, index);
            } else {
                numeric = false;
            }

            return numeric;
        }
        // 已经到了字符串的末尾了,说明其没有指数部分
        else {
            return true;
        }

    }

    /**
     * 判断是否是科学计数法的结尾部分,如E5,e5,E+5,e-5,e(E)后面接整数
     *
     * @param string 字符串
     * @param index  开始匹配的位置
     * @return 匹配的结果
     */
    private static boolean isExponential(String string, int index) {

        if (index >= string.length() || (string.charAt(index) != 'e' && string.charAt(index) != 'E')) {
            return false;
        }

        // 移动到下一个要处理的位置
        index++;

        // 到达字符串的末尾,就返回false
        if (index >= string.length()) {
            return false;
        }

        if (string.charAt(index) == '+' || string.charAt(index) == '-') {
            index++;
        }

        // 到达字符串的末尾,就返回false
        if (index >= string.length()) {
            return false;
        }

        index = scanDigits(string, index);

        // 如果已经处理到了的数字的末尾就认为是正确的指数
        return index >= string.length();
    }

    /**
     * 扫描字符串部分的数字部分
     *
     * @param string 字符串
     * @param index  开始扫描的位置
     * @return 从扫描位置开始第一个数字字符的位置
     */
    private static int scanDigits(String string, int index) {
        while (index < string.length() && string.charAt(index) >= '0' && string.charAt(index) <= '9') {
            index++;
        }
        return index;
    }
}

方法三:

思路:正则表达式
Java正则表达式的语法与示例

public class Solution {
    public boolean isNumeric(char[] str) {
        String string = String.valueOf(str);
        return string.matches("[\\+-]?[0-9]*(\\.[0-9]+)?([eE][\\+-]?[0-9]+)?"); //double d = -.123;
    }   
}

正则表达式样例:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Solution {
    public static void main(String[] args) {
        // 要验证的字符串
        String str = "baike.xsoftlab.net";
        // 正则表达式规则
        String regEx = "baike.";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regEx);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        // 查找字符串中是否有匹配正则表达式的字符/字符串
        boolean rs = matcher.find();
        boolean rm = matcher.matches();
        System.out.println(rs); //true
        System.out.println(rm); //false 全局匹配
        System.out.println(str.matches(regEx)); //false 全局匹配
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值