- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we extract the numbers from an input string in Java?
The java.lang.String class gives a considerable measure of methods to deal with a string. By the assistance of these methods, one can perform operations on the string like trimming, concatenating, converting and comparing. We can extract the numbers from a given string by using the replaceAll() method of a String class.
Example
import java.util.Scanner; public class StringExtractTest { public static void main(String[] args) { String input; String numbers; Scanner scanner = new Scanner(System.in); System.out.print(" Please enter a string from the keyboard that contains numbers: "); input = scanner.nextLine(); numbers = input.replaceAll("[^0-9]", ""); System.out.println("The Numbers are: " + numbers); } }
Output
Please enter a string from the keyboard that contains numbers: Welcome to Tutorials Point 1234567890 The Numbers are: 1234567890
- Related Articles
- How can we extract a substring from a string in MySQL?
- How can we read from standard input in Java?
- How to extract numbers from a string in Python?
- How to extract numbers from a string using Python?
- Can we read from JOptionPane by requesting input from user in Java?
- Extract decimal numbers from a string in Python
- How to extract an HTML tag from a String using regex in Java?
- How to extract numbers from a string using regular expressions?
- How we can extract data using VB Scripting from SAP
- How to extract multiple integers from a String in Java?
- How can we extract the Year and Month from a date in MySQL?
- How to extract certain substring from a string using Java?
- How to extract the first n characters from a string using Java?
- How to extract the last n characters from a string using Java?
- How can we extract a substring from the value of a column in MySQL table?

Advertisements