How to check a string starts with numeric number?

Java

Java Problem Overview


I have a string which contains alphanumeric character.

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

Thanks,

Java Solutions


Solution 1 - Java

See the isDigit(char ch) method:

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.

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

Solution 2 - Java

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

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

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

Solution 3 - Java

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 :-)!!!");
}
}
}

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.

Solution 4 - Java

This should work:

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

Solution 5 - Java

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.

Solution 6 - Java

Use a regex like ^\d

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSrinivasan View Question on Stackoverflow
Solution 1 - JavaJonView Answer on Stackoverflow
Solution 2 - JavaBrock WoolfView Answer on Stackoverflow
Solution 3 - JavaTomView Answer on Stackoverflow
Solution 4 - JavaarsView Answer on Stackoverflow
Solution 5 - JavashahkalpeshView Answer on Stackoverflow
Solution 6 - JavajoesliceView Answer on Stackoverflow