Java Program To Check if String Contains Digits

In this post, we will learn Java program to check if String contains digits.

Here I will discuss two methods to find the numeric in a string.

  1. Using regular expression
  2. Using isDigit() method

Java Program To Check if String Contains Digits

Method 1: Using Regular Expression

package demopkg;

public class CheckNumericValue {
    
    public static void main(String[] args) {
        
    
     String sInput = "Page Not Found - 404";
    
    // using regular expression
    
    System.out.println(sInput.replaceAll("[^0-9]",""));

    }
}

Output: 404

Explanation: Here we have used replaceAll() method to replace string value with null.

[^0-9] – Represent non-numeric value.

[0-9] – Represent numeric value

 


Method 2: Using isDigit() method

package demopkg;

public class CheckIsNumeric {
    
    public static void main(String[] args) {
        
        String sInput = "Page Not Found - 404";
        
        char[] c = sInput.toCharArray();
         
         StringBuffer sb =new StringBuffer();
         boolean flag =false;
         for (char ch: c) {
             
         if(Character.isDigit(ch)) {
                 
          sb.append(ch);
          flag = true;
                  		 
    }
                         
}
         if(flag) {
             			 
                 System.out.println(sb);
         }else {
             
             System.out.println("Not contains digits");
         }
    }
    
    
    

}

Output: 404

Explanation: isDigit() is the method of Character class. First I have converted string into array of character then using for-each loop iterate each character then in if condition I checked numeric value one by one using isDigits() method.


Java checks if string contains letters and numbers

In above program we have already used regular expression [^0-9] to find the letters in a given string, same like using [a-zA-Z] can be used to find letters in a given string.

package demopkg;

public class CheckNumericValue {
    
    public static void main(String[] args) {
        
    
     String sInput = "Internal Server Error - 404";
    
    // using regular expression
    
      System.out.println(sInput.replaceAll("[^a-zA-Z]"," "));
       System.out.println(sInput.replaceAll("[a-zA-Z]"," "));

    }
}

Output:

Internal Server Error

– – 404


How To Check if a string contains only digits Python

If a string only numbers if the string’s character consist 0 to 9 numeric value. For example – “04567” etc.

# Initialising string
ini_str1 = '1234556'
ini_str2 = 'ghk123bc'

# printing initial string
print ("Initial Strings : ", ini_str1, ini_str2)
  
# Using isdigit()
if ini_str1.isdigit():
    print ("String1 contains all numbers")
else:
    print ("String1 doesn't contains all numbers")
      
if ini_str2.isdigit():
    print ("String2 conatins all numbers")
else:
    print ("String2 doesn't contains all numbers")

Output:

Initial Strings : 1234556 ghk123bc
String1 contains all numbers
String2 doesn’t contains all numbers


Summary:

In this post we have learnt ‘Java program to check if String contains digits‘ using different ways.

Thanks for reading! Happy Learning 🙂


Must Read:

Java Program to Find Palindrome Number

Java Program to Find ArmStrong Number

Check if the String contains only unicode letters or digits in Java

 

Leave a Comment