How do I know if the first digit of a string is a number?

Asked

Viewed 1,989 times

25

I have a form where I need to validate the taxpayer number.

If you start with PT or if it is a number without an acronym I validated by the Portuguese finance algorithm, otherwise I do not validate.

What I wanted to know is how do I know if the first digit of string of taxpayer number is a number or not?

  • 2

    What’s missing is not a Integer.TryParse.

  • 1

    Why the android tag?

  • @ramaral because I thought it could come in handy to anyone looking for this on android. It is certain that it is a feature of Java whatever it is.

3 answers

27


It’s simple, you need to take the first character and use the ready function isDigit().

Character.isDigit(x.charAt(0))

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Given the utluiz observation in the comment below, if the rule is to consider that the digit is only Arabic decimal numbers, then just make a simple comparison:

x.charAt(0) >= '0' && x.charAt(0) <= '9'

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Obviously you can put this in a generic method to use anywhere. The method can receive the character that must be analyzed, which leaves well generic, or receive the string and the method itself choose the first character, which limits the use to the example quoted in the question, but avoids extra work on the call.

I would not go for other options that will not bring gains, only losses. They ran a test with some of them in C#, which should result in something similar to Java. This option to check only the Arabic numbers should be even faster and simple.

  • 2

    In fact, if you look at documentation of the method, will realize that digit is a more general concept than number. This method will include, for example, Arabic numerals as this.

  • 2

    Yeah, then I’d need to have a definition of whether this can be included or not as a digit.

21

If the idea is to check only numbers of 0 to 9, the simplest, fastest and efficient way is:

character >= '0' && character <= '9'

You can create a simple routine like this:

public class NumberUtils {
    public static boolean isNumber(char character) {
        return character >= '0' && character <= '9';
    }
}

And maybe add another routine to check the initial character, like this:

public static boolean startsWithNumber(String s) {
    return s != null && !s.isEmpty() && isNumber(s.charAt(0));
}

Other approaches

Character.isDigit

If you look at documentation of the method, will realize that digit is a more general concept than number.

This method will include, for example, Arabic numerals as this: ٢

Integer.parseInt

This method works well, but is only necessary to test a full number and not just a character.

Google Guava

The Guava library has the static method Ints.tryParse(String), which basically does the same thing Integer.parseInt, but without launching exception. If the String is not a number, null is returned.

However, I repeat that just as the above method is also recommended for full numbers and not just a character.

Regular expression

An effective alternative, but slower, but that is useful especially if there are other types of patterns.

Take an example:

public static boolean startsWithNumber(String s) {
    return s != null && s.matches("^\\d.*$");
}
  • 1

    Excellent response :)

11

Take the first digit and try to convert it. If it fails, it does not start with number. Something like this:

public boolean primeiroDigitoEhUmNumero(String entrada) {
    String primeiroDigito = entrada.charAt(0) + "";
    try {
        Integer.parseInt(primeiroDigito);
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}
  • 6

    I’m sure it works, but I don’t think it’s a good idea to use exceptions to make comparisons.

  • 4

    I think so too, but I didn’t until now isDigit . Living and learning. :)

  • 1

    that’s how it is, always learning :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.