String matches() Method in Java with Examples Last Updated : 20 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In Java, the matches() method in the String class checks if a string matches a specified regular expression. It is useful for validating input patterns and searching within strings. In this article, we will learn how to use the matches() method effectively in Java with examples to illustrate its functionality.Example:In this example, we will check if a string contains only digits. Java // Java program to demonstrate matches() method import java.util.*; public class StringMatches { public static void main(String[] args) { // Declare and initialize a String String s = "12345"; // Check if the string matches the regex for digits boolean r = s.matches("\\d+"); // Matches one or more digits System.out.println("" + r); } } Outputtrue Table of ContentSyntax of matches() methodOther Examples of String matches() MethodCheck for Alphabets using matches() Validate an Email Address using matches()Check a String Pattern using matches()Syntax of matches() methodboolean matches(String regex)Parameter: regex: The regular expression to match against the string.Return Type:It returns true if the string matches the regex, otherwise false.Other Examples of String matches() Method1. Check for Alphabets using matches() We will use matches() method to check if a string contains only alphabetic characters. Java // Java program to check if a string contains only alphabets import java.util.*; public class StringMatches { public static void main(String[] args) { String s = "JavaProgramming"; // Check if the string contains only alphabets boolean r = s.matches("[a-zA-Z]+"); // Matches only alphabets (case-insensitive) System.out.println("" + r); } } Outputtrue 2. Validate an Email Address using matches()To validate an email addresses, regular expressions can be used with matches() method. Java // Java program to validate an email address import java.util.*; public class StringMatches { public static void main(String[] args) { String email = "[email protected]"; // Check if the string is a valid email address boolean r = email.matches("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"); System.out.println("" + r); } } Outputtrue 3. Check a String Pattern using matches()To check whether a string follows a specific pattern like combination of digits or letters, we can use matches() method of String class. Java // Java program to check if a string follows a specific pattern import java.util.*; public class StringMatches { public static void main(String[] args) { String s = "Java123"; // Check if the string contains alphabets followed by digits boolean r = s.matches("[A-Za-z]+\\d+"); System.out.println("" + r); } } Outputtrue Points to RememberThe matches() method is an efficient way to check if a string is in a specified pattern.By default, it is case-sensitive.Regular expressions are required for defining the patterns. Comment More infoAdvertise with us Next Article String matches() Method in Java with Examples A Astha Tyagi Follow Improve Article Tags : Strings Java DSA Java-Strings Java-lang package Java-Functions java-regular-expression +3 More Practice Tags : JavaJava-StringsStrings Similar Reads Matcher toString() method in Java with Examples The toString() method of Matcher Class is used to get the String representation of this matcher. This method is derived from the Object Class and behaves in the similar way. Syntax: public String toString() Parameters: This method takes no parameters. Return Value: This method returns a String value 2 min read Year toString() method in Java with Examples The toString() method of Year class in Java is used to return the string representation of this Year object. Syntax: public String toString() Parameter: This method does not accepts any parameter. Return Value: It returns the string representation of this Year object. Below programs illustrate the t 1 min read Byte toString() method in Java with examples The toString() method of Byte class is a built in method in Java which is used to return a String value. public String toString() Syntax: ByteObject.toString() Return Value: It returns a String object, the value of which is equal to the value of the ByteObject. Below is the implementation of toStrin 2 min read String replace() method in Java with Examples The String replace() method returns a new string after replacing all the old characters/CharSequence with a given character/CharSequence. Example:Return a new string where all " o" characters are replaced with "p" character: Java // Java program to demonstrate // the replace() method public class Ma 4 min read Class toString() method in Java with Examples The toString() method of java.lang.Class class is used to convert the instance of this Class to a string representation. This method returns the formed string representation. Syntax: public String toString() Parameter: This method does not accept any parameter. Return Value: This method returns the 1 min read Field toString() method in Java with Examples The toString() method of java.lang.reflect.Field is used to get a string describing this Field. The format of the returned string is the access modifiers for the field, if any, followed by the field type, followed by a space, followed by the fully-qualified name of the class declaring the field, fol 3 min read List size() method in Java with Examples The size() method of the List interface in Java is used to get the number of elements in this list. That is, the list size() method returns the count of elements present in this list container.Example:Java// Java Program to demonstrate // List size() Method import java.util.*; class GFG { public sta 2 min read String toString() Method in java with Examples String toString() is the built-in method of java.lang which return itself a string. So here no actual conversion is performed. Since toString() method simply returns the current string without any changes, there is no need to call the string explicitly, it is usually called implicitly. Syntax : publ 1 min read Stack toString() method in Java with Example The toString() method of Java Stack is used to return a string representation of the elements of the Collection. The String representation comprises a set representation of the elements of the Collection in the order they are picked by the iterator closed in square brackets[].This method is used mai 2 min read Scanner toString() method in Java with Examples The toString() method of java.util.Scanner class returns the string representation of this Scanner. The exact format is unspecified. Syntax: public String toString() Return Value: This function returns the string representation of this scanner. Below program illustrates the above function: Program 1 1 min read Like