ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.