Home » Tip Bank » Java
  • Comment on this Article
Language: Java
Expertise: Intermediate
Jan 4, 2019
  • facebook
  • twitter
  • google+
  • in
  • Email

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Watch

Finding All Digits in a Given String

This regex will help you retrieve all the digits that are present in the string.

Regex is powerful and makes this a simple procedure.

public class FindAllDigitsInString
{
   public static void main(String args[])
   {
      FindAllDigitsInString findAllDigitsInString = new FindAllDigitsInString();
      findAllDigitsInString.proceed();
   }

   private void proceed()
   {
      String alphaNumericString = "No#10,Prime Conclave,56014";
      String allDigitsInString= alphaNumericString.replaceAll("[^0-9]", "");      
   
      System.out.println("All the digits in string: " + allDigitsInString);
   }
   
}

/*

Expected output:

[root@mypc]# java FindAllDigitsInString 
All the digits in String: 1056014

*/
M S Sridhar
 
Submit a Tip
Browse "Java" Tips
Browse All Tips
htmlgoodies.com
googlecode.com