Open In App

How to Use String.matches() as a Substitute of startWith() Method in Java?

Last Updated : 06 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

String startsWith() as the syntax suggests this method is present in String class which is responsible for testing if a string starts with the specified prefix beginning from the first index.

Syntax:

public boolean startsWith(String prefix)

Parameters: The prefix to be matched.

Return Value: It returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise.

Methods:

There exist two substitutes of startWith()  as listed below:

  1. Regular expressions
  2. matches() method of String class 

Let us discuss both methods which are as follows: 

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:

Example

Java




// java Program to Illustrate Usage of Regular Expressions
// as a Substitute of startWith() method
 
// Importing Matcher and Pattern classes
// from the java.util.regex package
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Declaring and initialising custom string
        String Str = new String("Welcome to geeksforgeeks");
 
        // Display message for better readability
        System.out.print(
            "Check whether string starts with Welcome using startsWith : ");
 
        // Testing the prefix using startsWith()
        System.out.println(Str.startsWith("Welcome"));
 
        // Testing the prefix using Regex() by creating
        // objects of Pattern and Matcher class
        Pattern pattern = Pattern.compile("Welcome.*");
        Matcher m = pattern.matcher(Str);
 
        // Print commands
        System.out.print(
            "Check whether string starts with Welcome using Regex: ");
        System.out.println(m.find() ? "true" : "false");
    }
}


Output

Check whether string starts with Welcome using startsWith : true
Check whether string starts with Welcome using Regex: true

Method 2: Using matches() method of String class 

String.matchs() 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 Usage of String.matches()
// as a Substitute of startWith() method
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Declaring and initialising string
        String Str = new String("Welcome to geeksforgeeks");
 
        // Testing the prefix using startsWith()
        // Work taken - 'Welcome'
        System.out.print(
            "Check whether string starts with Welcome using startsWith : ");
        System.out.println(Str.startsWith("Welcome"));
 
        // Testing the prefix using Regex()
        // Word to be matched - 'Welcome'
        System.out.print(
            "Check whether string starts with Welcome using Matches: ");
        System.out.println(Str.matches("Welcome(.*)"));
    }
}


Output

Check whether string starts with Welcome using startsWith : true
Check whether string starts with Welcome using Matches: true

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads