Java 如何检查字符串以数字开头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1107798/
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-11 23:50:53  来源:igfitidea点击:

How to check a string starts with numeric number?

java

提问by

I have a string which contains alphanumeric character.

我有一个包含字母数字字符的字符串。

I need to check whether the string is started with number.

我需要检查字符串是否以数字开头。

Thanks,

谢谢,

回答by joeslice

Use a regex like ^\d

使用正则表达式 ^\d

回答by Jon

See the isDigit(char ch)method:

isDigit(char ch)方法:

https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Character.html

https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Character.html

and pass it to the first character of the String using the String.charAt()method.

并使用该String.charAt()方法将其传递给 String 的第一个字符。

Character.isDigit(myString.charAt(0));

回答by shahkalpesh

System.out.println(Character.isDigit(mystring.charAt(0));

EDIT: I searched for java docs, looked at methods on string class which can get me 1st character & looked at methods on Character class to see if it has any method to check such a thing.

编辑:我搜索了 java 文档,查看了字符串类上的方法,它可以让我获得第一个字符并查看 Character 类上的方法,看看它是否有任何方法来检查这样的事情。

I think, you could do the same before asking it.

我想,你可以在问它之前做同样的事情。

EDI2: What I mean is, try to do things, read/find & if you can't find anything - ask.
I made a mistake when posting it for the first time. isDigit is a static method on Character class.

EDI2:我的意思是,尝试做事,阅读/查找,如果你找不到任何东西——问。
第一次发帖的时候弄错了。isDigit 是 Character 类的静态方法。

回答by Brock Woolf

Sorry I didn't see your Java tag, was reading question only. I'll leave my other answers here anyway since I've typed them out.

抱歉,我没有看到您的 Java 标签,只是在阅读问题。反正我会在这里留下我的其他答案,因为我已经把它们打出来了。

Java

爪哇

String myString = "9Hello World!";
if ( Character.isDigit(myString.charAt(0)) )
{
    System.out.println("String begins with a digit");
}

C++:

C++

string myString = "2Hello World!";

if (isdigit( myString[0]) )
{
    printf("String begins with a digit");
}

Regular expression:

正则表达式

\b[0-9]

Some proof my regex works:Unless my test data is wrong? alt text

一些证明我的正则表达式有效:除非我的测试数据是错误的? 替代文字

回答by ars

This should work:

这应该有效:

String s = "123foo";
Character.isDigit(s.charAt(0));

回答by Tom

I think you ought to use a regex:

我认为你应该使用正则表达式:


import java.util.regex.*;

public class Test {
  public static void main(String[] args) {
    String neg = "-123abc";
    String pos = "123abc";
    String non = "abc123";
        /* I'm not sure if this regex is too verbose, but it should be
         * clear. It checks that the string starts with either a series
         * of one or more digits... OR a negative sign followed by 1 or
         * more digits. Anything can follow the digits. Update as you need
         * for things that should not follow the digits or for floating
         * point numbers.
         */
    Pattern pattern = Pattern.compile("^(\d+.*|-\d+.*)");
    Matcher matcher = pattern.matcher(neg);
    if(matcher.matches()) {
        System.out.println("matches negative number");
    }
    matcher = pattern.matcher(pos);
    if (matcher.matches()) {
        System.out.println("positive matches");
    }
    matcher = pattern.matcher(non);
    if (!matcher.matches()) {
        System.out.println("letters don't match :-)!!!");
    }
  }
}

You may want to adjust this to accept floating point numbers, but this will work for negatives. Other answers won't work for negatives because they only check the first character! Be more specific about your needs and I can help you adjust this approach.

您可能想要调整它以接受浮点数,但这将适用于负数。其他答案对否定无效,因为它们只检查第一个字符!更具体地了解您的需求,我可以帮助您调整这种方法。