How do we find out if first character of a string is a number in java?



In this tutorial, we will learn how to find out if the first character of a String is a digit in Java. Following are the different ways to do so:

Using the isDigit() method

The isDigit() method of the java.lang.Character class accepts a character as a parameter and determines whether it is a digit or not. If the given character is a digit, this method returns true otherwise, this method returns false.

Therefore, to determine whether the first character of the given String is a digit.

  • The charAt() method of the String class accepts an integer value representing the index and returns the character at the specified index.
  • The toCharArray() method of this class converts the String to a character array and returns it You can get its first character as array[0].
  • Retrieve the 1 st;character of the desired String using either of the methods.
  • Then, determine whether it is a digit or not by passing it as a parameter to the isDigit() method.

Example

Following is the Java program to find out if the first character of a String is a digit:

public class FirstCharToDgit{
   public static void main(String args[]){
      String str = "1Aishwarya";
      
      char[] ch = str.toCharArray();

      char c = ch[0];

      if(Character.isDigit(c)){
         System.out.println("First character is a digit");
      }else{
         System.out.println("First character is not a digit");
      }
   }
}

Output

First character is a digit

Using the regular expressions.

The matches() method of the String class accepts a regular expression and verifies if it matches the current String; if so, it returns true, else it returns false.

The regular expression to match a String that contains a digit as its first character is "^[0-9].*$". Pass this as a parameter to the matches() method of the String class.

Following are the steps to determine whether the first character of a String is a digit using the regular expressions:

  • Declare a String variable and assign it the desired String.
  • Use the matches() method of the String class to check if the first character of the String is a digit.
  • Pass the regular expression "^[0-9].*$" as a parameter to the matches() method.
  • If the matches() method returns true, then the first character of the String is a digit.

Example

Following is the Java program to find out if the first character of a String is a digit using the regular expressions:

public class IsFirstCharADigit{
   public static void main(String args[]){
      String str = "Aishwarya";
      
      if(str.matches("^[0-9].*$")){
         System.out.println("First character is a digit");
      }else{
         System.out.println("First character is not a digit");
      }
   }
}

Output

When the above code is executed, it produces the following output:

First character is not a digit

Using the charAt() and isDigit() methods

The charAt() method is part of the String class and used to get the character at a particular index of the String.

Steps are as follows:

  • Use the charAt() method of the String class to get the first character of the String.
  • Pass the first character as a parameter to the isDigit() method of the java.lang.Character class.
  • If the isDigit() method returns true, then the first character of the String is a digit.

Example

Following is the Java program to find out if the first character of a String is a digit using the charAt() method:

public class IsFirstCharADigit{
   public static void main(String args[]){
      String str = "1Aishwarya";
      
      char c = str.charAt(0);

      if(Character.isDigit(c)){
         System.out.println("First character is a digit");
      }else{
         System.out.println("First character is not a digit");
      }
   }
}

Output

When the above code is executed, it produces the following output:

First character is a digit
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-09-01T12:40:40+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements