
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How do we find out if first character of a string is a number in java?
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 else, 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 1st 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
import java.util.Scanner; public class FirstCharacterOfString { public static void main(String args[]) { //Reading String from user System.out.println("Enter a String"); Scanner sc = new Scanner(System.in); String str = sc.next(); //Converting String to a character array char charArray[] = str.toCharArray(); boolean bool = Character.isDigit(charArray[0]); if(bool) { System.out.println("First character is a digit"); } else { System.out.println("First character is not a digit"); } } }
Output1
Enter a String krishna First character is not a digit
Output2
Enter a String 2sample First character is a digit
Using the regular expressions.
The matches() method of the String class accepts a regular expression and verifies it matches with the current String, if so, it returns true else, it returns false.
The regular expression to match String which contains a digit as first character is “^[0-9].*$”. Pass this as a parameter to the matches() method of the String class.
Example
import java.util.Scanner; public class FirstCharacterOfString { public static void main(String args[]) { //reading String from user System.out.println("Enter a String"); Scanner sc = new Scanner(System.in); String str = sc.next(); boolean bool = str.matches("^[0-9].*$"); if(bool) { System.out.println("First character is a digit"); } else { System.out.println("First character is not a digit"); } } }
Output1
Enter a String krishna First character is not a digit
Output2
Enter a String 2sample First character is a digit
- Related Articles
- How do we split a string on a fixed character sequence in java?
- How to find the first character of a string in C#?
- How do we check if a String contains a substring (ignoring case) in Java?
- How to find out if a Python object is a string?
- How to print the first character of each word in a String in Java?
- How to check if first character in a cell is a letter or number in Excel?
- How to check if a given character is a number/letter in Java?
- How do I identify if a string is a number in C#?
- Find repeated character present first in a string in C++
- Find the first repeated character in a string using C++.
- How to find a unique character in a string using java?
- Java Program to Capitalize the first character of each word in a String
- How do we create a string from the contents of a file in java?
- How do we compare String in Java
- How to find its first non-repeating character in a given string in android?
