Java Program To Extract Digits / Numbers From String
Chapter:
Interview Programs
Last Updated:
22-09-2018 08:53:27 UTC
Program:
/* ............... START ............... */
import java.util.Scanner;
public class JavaExtractDigitsFromStringf {
public static void main(String[] args) {
String str;
String numbers;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter string that contains numbers: ");
str = scanner.nextLine();
// extracting string
numbers = str.replaceAll("[^0-9]", "");
System.out.println("Numbers are: " + numbers);
}
}
/* ............... END ............... */
Output
Enter string that contains numbers: javas12can.com89389
Numbers are: 1289389
Notes:
-
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.
Tags
Program To Extract Digits / Numbers From String, Java, Interview, java extract number from string regex, how to extract numbers from alphanumeric string in java, java program to extract digits numbers from the string