
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get All Digits from a String in Java
Let’s say the following is our string.
String str = "DEMO98 TE4567XT";
To display only the digits from the above string, we have used the replaceAll() method and replace all the characters with empty.
str.replaceAll("\D", ""))
The following is the final example that displays only digits from the string.
Example
public class Demo { public static void main(String[] args) { String str = "DEMO98 TE4567XT"; System.out.println("String = "+str); System.out.println("Displaying digits: "+str.replaceAll("\D", "")); } }
Output
String = DEMO98 TE4567XT Displaying digits: 984567
Advertisements