dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Finding All Digits in a Given String

See how this regex will help you retrieve all the digits that are present in a string.


advertisement

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

*/

 

Visit the DevX Tip Bank

 



   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date