Understanding the Java String Matches Method

java string matches

Strings handling and manipulation is integral to any software application. Be it a local departmental store inventory system that keeps track of sales and purchases or an enterprise level web-based airline ticket reservation system, string manipulation is involved at every stage of application development process. Usually, in traditional programming languages like C, strings are treated as arrays of characters. However, things are quite different in case of Java where Strings are implemented in the form of classes. This mechanism allows developers to write utility functions that can be used right away from Java libraries to handle string type objects. One such extremely useful method is the Java String Matches method that matches a string against a condition specified with the help of Java Regular expressions.

Interested in learning more about Java? Take a course at Udemy.com.

What is Java String Matches Method?

Java String matches is an instance method of the Java String class and is used to perform various condition matching functionalities. For instance, Java String matches method, can be used to check if a string contains alphabets from u to x. Or, it can be used to find if a string contains particular digit more than 5 times or if a string contains a prime number or not. Basically, a condition is passed to the Matches method in the form of regular expression and the String on which Java String Matches method has been called is matched with this condition.

How Java String Matches Method Works?

Having a basic understanding of Java Regular Expressions is the prerequisite to comprehending the Java String Matches method. In the following example, Java String Matches method has been explained with a bit of information about the Java Regular Expression, commonly known as regex.

The syntax of the Java String matches method is as follows:

boolean matches(String regex)

For More interesting Java String tutorials, take a course at Udemy.com.

Java String Matches: A basic Example

Example 1

String [] str = {"Udemy", "Java", "Tutorials", "for", "Java", "Beginners"};

for(String s : str)

{

boolean matches = s.matches(".*[1-9].*");

System.out.println("Does "+s +" contains any digit?" + matches);

}

In the above example, the String type array str is declare. The string contains some random strings literals. In the next line of code, a for loop is implemented that iterates through all the elements in the str array. Inside the for loop, Java String Matches method has been called on each element in the string str.

The regular expression that has been passed to the Java String Matches method is (“.*[1-9].*”). This regular expression is basically a string. The dot “.” in the string signifies that there can be any character before the string in the bracket and the steric “*” signifies that these characters can be in any number. The values in the bracket 1-9 are the set of characters that are to be matched. Finally on the right side of the bracket “.*” has been inserted again.

Putting the above regular expression in plain English, the Matches method will return true if it found any numeric digit from 1-9 in the elements of the string type array str. Since, none of the elements of str array contains any numeric digit, therefore all the matches methods would return false which would be displayed on the console output. The output of the aforementioned code would look like this:

Does Udemy contains any digit? false

Does Java contains any digit? false

Does Tutorials contains any digit? false

Does for contains any digit? false

Does Java contains any digit? false

Does Beginners contains any digit? false

It is evident from the output that none of the String in str array matched the condition of having a numeric digit of 1-9.

Example 2

Make following modification in the last example and then see the output of the Java String Matches method with same regular expression:

String [] str = {"Udemy5", "Java", "6Tutorials", "for", "Ja2va","Beginners"};

for(String s : str)

{

boolean matches = s.matches(".*[1-9].*");

System.out.println("Does "+s +" contains any digit?" + matches);

}

In the above code, three elements in the String type array str contain numeric digits. These elements are “Udemy5”, “6Tutorials” and “Ja2va”. Now, the Java String Matches method with regular expression “.*[1-9].*” would return true thrice, one each for the three string with numeric digit from 1-9 between them. The output of the code in Example2 would be:

Does Udemy5 contains any digit? true

Does Java contains any digit? false

Does 6Tutorials contains any digit? true

Does for contains any digit? false

Does Ja2va contains any digit? true

Does Beginners contains any digit? false

You can see that Matches method has returned true when it is called on string elements “Udemy5”, “6Tutorials” and “Ja2va” since they contain a numeric digit in them.

A very interesting point to note in Example 2 is that Matches method has returned true for all elements containing a numeric digit, irrespective of the location of the numeric digit. For instance in “Udemy5”, numeric digit is the last character, in “6Tutorials” it is the first character and in “Ja2va” the numeric digit is the middle character.

However, sometimes, situation arises where first or element character needs to be matched. For instance, what to do if string is matched for all those elements that end with a numeric digit. In such cases regular expression needs to be modified a bit. This concept has been explained in the next example.

Example 3

String [] str = {"Udemy5", "Java", "6Tutorials", "for", "Ja2va","Beginners"};

for(String s : str)

{

boolean matches = s.matches(".*[1-9]");

System.out.println("Does "+s +" contains any digit?" + matches);

}

In the above example, the regular expression has been slightly modified and “.*” has been omitted from the end. This means that match the characters 1-9 in all the elements and there should be no element after the matched character. In other words, check if the last character of the element is a numeric digit or not. In this case only “Udemy5” has the last character as the numeric string, therefore Java String matched method would return true for this element only. The output of this example will be as follows:

Does Udemy5 contains any digit? true

Does Java contains any digit? false

Does 6Tutorials contains any digit? false

Does for contains any digit? false

Does Ja2va contains any digit? false

Does Beginners contains any digit? false

To learn essentials of Java language, take a course at Udemy.com.