How to check if a String has digits in Java

In this blog post, I will be explaining how you can check if a String has digits. There are several ways to do this. Consider the following code snippet:

 

package learnjava.strings;import java.util.regex.Pattern;public class CheckIfStringHasDigits {public static void main(String[] args) {String str = "Hello123";String regex = ".*[0-9].*";//Method 1 - Using String.matchesboolean matches = str.matches( regex );System.out.println("Using String.matches="+matches);//Method 2 - Using pattern.matchesmatches = Pattern.matches( regex , str);System.out.println("Using pattern.matches="+matches);//Method 3 - using pattern.compilematches = Pattern.compile( regex).matcher( str ).find();System.out.println("Using Pattern.compile="+matches);//Method 4 - Checking each characterboolean digitFound = false;char[] characters = str.toCharArray();for(char c:characters){if(Character.isDigit(c)){digitFound = true;break;}}System.out.println("Using manual method="+digitFound);}}

 

Method 1

This method uses the String.matches method. This method returns true if the String on which it is invoked matches the regular expression passed.

Method 2:

This method uses the Pattern.matches method. This method accepts a regular expression and String. It returns true if the regular expression matches the input String.

Method 3

This method uses Pattern.compile.matcher.find. This behaves in exactly the same way as Pattern.matches method. However, here the regular expression is compiled separately. If you are using the same regular expression multiple times, compiling it once and using it will give some performance benefits.

Method 4:

This method manually checks each character in the input String. The moment it finds a digit, it returns a true to indicate that there is a digit in the input String.

Comments

Popular posts from this blog

How to use logging in SpringBoot with code samples

Python While Loop with code samples

How to convert a List to a Set