Q:

Java program to extract digits/ numbers from the string

0

Java program to extract digits/ numbers from the string

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Core Java Example programs for Beginners and Professionals

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to run an application - Run Exe using... >>
<< Java program to print all prime numbers from 1 to ...