by Dinesh Thakur

boolean matches (String regex): This method tests whether or not the invoking string matches the given regular expression regex. It returns true if invoking string matches the given regular expression otherwise it returns false.It is different from the equals () method where you can only use the fixed strings. Forexample,

" DineshThakur".matches("Dinesh") // returns false

" DineshThakur".matches("Dinesh.*") // returns true

public class StringMatches

{
       public static void main(String[] args)
    {
            String s1 = "DineshThakur" ;
            System.out.println("Use of matches() methods");
            System.out.println("s1.matches(\"Dinesh\") : " + s1.matches("Dinesh"));
            System.out.println("s1.matches(\"Dinesh.*\") : " + s1.matches("Dinesh.*"));
     }
}

String matches() Methods in Java