How to Use String Matches() in Java


In this example, we will learn how to use String.matches() in Java.

Source Code

package com.beginner.examples;

public class MatchesExample {
     
    public static void main(String a[]){
    	String str = "1";
    	//String.matches() method tells whether or not this string matches the given regular expression.
    	System.out.println(str.matches("(\d+)"));
    	
    	str = "1a";
    	//String.matches() method tells whether or not this string matches the given regular expression.
    	System.out.println(str.matches("(\d+)"));
    }
}

Output:

true
false
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments