×

Java Programs

Java Practice

Java program to extract digits/ numbers from the string

This program will extract the digits, numbers from the string using java program, in this program we will read a string that will contain digits in the string and will extract the digits, numbers from the inputted string.

Extract Digits/ Numbers from String using Java Program

//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

Core Java Example Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.