tags:

views:

1951

answers:

6

Hi all,

I have a string which contains alphanumeric character.

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

Thanks,

A: 

Use a regex like ^\d

joeslice
+8  A: 

See the isDigit(char ch) method:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html

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

Character.isDigit(myString.charAt(0));
Jon
Just for completeness: isDigit returns true not just 0..9 but also other (e.g. arabic) digits as well: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html#isDigit(char)
Csaba_H
as do most methods that are named isXXX(). I think that's implicit in the method name and can be found by looking at the link to the method that I provided...
Jon
@Csaba_H - FYI: 0-9 are Arabic numerals - but I get your point.
McDowell
A: 
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.

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.

shahkalpesh
Good suggestion. +1 for that.
Adeel Ansari
A: 
Brock Woolf
Brock: OP has added java tag (not specified in the question).
shahkalpesh
Perhaps now that I have fixed my answer the person who down voted me will reconsider?
Brock Woolf
thank you Sir =)
Brock Woolf
I believe that regex is wrong. Not for the reasons I put in my answer (because I'm not sure I am right, because it depends on what the OP intended... he needs to clarify), but because of the \b. \b is a word boundary. You want to force the digit at the beginning of the string, so you should use ^.
Tom
Well, the guy is a one time hit from Google and obviously doesn't understand how Stackoverflow works. He's had a ton of answers and hasn't marked one correct. I'd let it go mate.
Brock Woolf
Sorry Brock, I stand corrected. I thought the test data might have been wrong, because if a string started with a space, I thought that was a word boundary... I tried it out though, and it seems to work fine :-).
Tom
+1  A: 

This should work:

String s = "123foo";
Character.isDigit(s.charAt(0));
ars
+2  A: 

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.

Tom
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
Johan
That's an awfully complex solution for "is the first character a digit"
skaffman
+1 the question was ambiguous and didn't specify "is the first character a digit"
Pablojim
@skaffman: if you read my answer, it's pretty obvious I wasn't trying to answer "is the first character a digit"... which is *NOT* what the OP wrote. The OP asked: "string is started with number". I was trying to make a more robust solution that handled negatives. It's pretty clear that the question is unclear. (Read my comments on other posts). I'm not sure my answer really deserved a downvote (although I'm not sure it was you that downvoted). Also, the solution looks complex, but for it's really not... it's mostly garbage code for print statements to show what it matches properly.
Tom