Java Program to Extract Digits/ Numbers From the String

You are currently viewing Java Program to Extract Digits/ Numbers From the String

Java program to extract digits/ numbers from the string

//Java program to extract numbers/digits from a string.
 
import java.util.Scanner;
  
class ExtractNumberFromString
{
  public static void main(String[] args) 
  {
      String str;
      String numbers;
       
      Scanner SC=new Scanner(System.in);
       
      System.out.print("Enter string that contains numbers: ");
      str=SC.nextLine();
       
      //extracting string
      numbers=str.replaceAll("[^0-9]", "");
       
      System.out.println("Numbers are: " + numbers);
  }
}

Output

    
    Enter string that contains numbers: Hello World 1234567890 Hi
    Numbers are: 1234567890

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply