Here we are going to see a code sample which would help to determine if a string contains only numeric digits or not. We can also find the number of digits it contains. So we will also look at the different ways to find numeric digits in String as well as count of digits in the String. To do so, Java provides regular expressions using java.util.regex.Pattern and java.util.regex.Matchter class.
Complete Code Sample to check if String contains only numeric digits
Output for the above program is :
The code is self explanatory and I have given the comments in code itself to make more clear. One to note that you can change the number of digits in regular expression based on your need. In the above example it is 7 but you can put any number you want and it would look for those number of digits only in your string.