How to Use Regular Expression as a Substitute of endsWith() Method in Java?

17 Dec, 2021
So primarily discuss what is endsWith() method is, so it is a method of String class that checks whether the string ends with a specified suffix. This method returns a boolean value true or false.
Syntax:
public boolean endsWith(String suff)      
Parameter: specified suffix part  
Return: Boolean value, here in java we only have true and false.
Methods:
We can use Regular Expression / Matches as a substitute for endsWith() method  in Java
Using Regular ExpressionUsing Matches
Using Regular Expression
Using Matches
Method 1: Using Regular Expression
Regular Expressions or Regex (in short) is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are a few areas of strings where Regex is widely used to define the constraints. Regular Expressions are provided under java.util.regex package. This consists of 3 classes and 1 interface. The java.util.regex package primarily consists of the following three classes as depicted below in tabular format as follows:
Pattern
Matcher
PatternSyntaxException
Example
Java
// Java Program to Illustrate use of Regular Expression// as substitute of endsWith() method // Importing required classesimport java.util.regex.Matcher;import java.util.regex.Pattern; // Main classpublic class Geek {     // Main driver method    public static void main(String args[])    {         // Declaring and initialising string        String Str = new String("Welcome to geeksforgeeks");         // Display message        System.out.print(            "Check whether string ends with Welcome using endsWith : ");         // Using endWith() method        System.out.println(Str.endsWith("geeks"));         // Creating Pattern and matcher classes object        Pattern pattern = Pattern.compile(".*geeks");        Matcher m = pattern.matcher(Str);         // Checking whether string ends with specific word        // or nor and returning the boolean value        // using ? operator and find() method        System.out.print(            "Check whether string ends with Welcome using Regex: ");        System.out.println(m.find() ? "true" : "false");    }}
Check whether string ends with Welcome using endsWith : true
Check whether string ends with Welcome using Regex: true
 Method 2: Using Matches
String.matches() method tells whether or not this string matches the given regular expression. An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression Pattern.matches(regex, str).
Syntax:
public boolean matches(String regex);
Parameters: The regular expression to which this string is to be matched.
Return Value: This method returns true if, and only if, this string matches the given regular expression.
Example 
Java
// Java Program to Illustrate use of Matches// as substitute of endsWith() method // Importing all utility classesimport java.util.*; // Main classpublic class Geek {     // Main driver method    public static void main(String args[])    {         // Initialising String        String Str = new String("Welcome to geeksforgeeks");         // Display message for better readability        System.out.print(            "Check whether string starts with Welcome using endsWith : ");        // Using endsWith() to end with specific word        System.out.println(Str.endsWith("geeks"));         // Display message for better readability        System.out.print(            "Check whether string starts with Welcome using Matches: ");        // Now checking whether it matches with        // above defined specific word        System.out.println(Str.matches("(.*)geeks"));    }}
Check whether string starts with Welcome using endsWith : true
Check whether string starts with Welcome using Matches: true
akshitsaxenaa09
simranarora5sos
Blogathon-2021
java-regular-expression
Blogathon
How To
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Import JSON Data into SQL Server?
SQL Query to Convert Datetime to Date
Python program to convert XML to Dictionary
Scrape LinkedIn Using Selenium And Beautiful Soup in Python
Shared ViewModel in Android
How to Install PIP on Windows ?
How to Find the Wi-Fi Password Using CMD in Windows?
How to install Jupyter Notebook on Windows?
Java Tutorial
How to filter object array based on attributes?