Java 如何确定字符串的第一个字符是否为数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1223052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:05:26  来源:igfitidea点击:

How do I find out if first character of a string is a number?

javastring

提问by Omnipresent

In Java is there a way to find out if first character of a string is a number?

在 Java 中有没有办法找出字符串的第一个字符是否是数字?

One way is

一种方法是

string.startsWith("1")

and do the above all the way till 9, but that seems very inefficient.

并一直执行上述操作直到 9,但这似乎非常低效。

采纳答案by Michael Myers

Character.isDigit(string.charAt(0))

Note that this will allow anyUnicode digit, not just 0-9. You might prefer:

请注意,这将允许任何Unicode 数字,而不仅仅是 0-9。您可能更喜欢:

char c = string.charAt(0);
isDigit = (c >= '0' && c <= '9');

Or the slower regex solutions:

或较慢的正则表达式解决方案:

s.substring(0, 1).matches("\d")
// or the equivalent
s.substring(0, 1).matches("[0-9]")

However, with any of these methods, you must first be sure that the string isn't empty. If it is, charAt(0)and substring(0, 1)will throw a StringIndexOutOfBoundsException. startsWithdoes not have this problem.

但是,对于这些方法中的任何一种,您必须首先确保字符串不为空。如果是,charAt(0)并且substring(0, 1)会抛出一个StringIndexOutOfBoundsException. startsWith没有这个问题。

To make the entire condition one line and avoid length checks, you can alter the regexes to the following:

要使整个条件成为一行并避免长度检查,您可以将正则表达式更改为以下内容:

s.matches("\d.*")
// or the equivalent
s.matches("[0-9].*")

If the condition does not appear in a tight loop in your program, the small performance hit for using regular expressions is not likely to be noticeable.

如果条件没有出现在您的程序中的紧密循环中,则使用正则表达式的小性能损失可能不会很明显。

回答by Narayan Yerrabachu

regular expression starts with number->'^[0-9]' 
Pattern pattern = Pattern.compile('^[0-9]');
 Matcher matcher = pattern.matcher(String);

if(matcher.find()){

System.out.println("true");
}

回答by Kiril Aleksandrov

Regular expressions are very strong but expensive tool. It is valid to use them for checking if the first character is a digit but it is not so elegant :) I prefer this way:

正则表达式是非常强大但昂贵的工具。使用它们来检查第一个字符是否是数字是有效的,但它不是那么优雅:) 我更喜欢这种方式:

public boolean isLeadingDigit(final String value){
    final char c = value.charAt(0);
    return (c >= '0' && c <= '9');
}

回答by Pedro H. N. Vieira

I just came across this question and thought on contributing with a solution that does not use regex.

我刚刚遇到这个问题,并考虑为不使用正则表达式的解决方案做出贡献。

In my case I use a helper method:

就我而言,我使用辅助方法:

public boolean notNumber(String input){
    boolean notNumber = false;
    try {
        // must not start with a number
        @SuppressWarnings("unused")
        double checker = Double.valueOf(input.substring(0,1));
    }
    catch (Exception e) {
        notNumber = true;           
    }
    return notNumber;
}

Probably an overkill, but I try to avoid regex whenever I can.

可能有点矫枉过正,但我​​尽量避免使用正则表达式。