Java String matches() Method

Java String matches() Method

What is strings in Java?

A string is a sequence of characters that is stored as a char array and is represented by the String class. Strings are commonly used to store and display text such as names, addresses, and other kinds of information. They are also used to store and manipulate data read from external sources, such as text files or user input.

  • Strings are also widely used in Java for formatting and parsing data. For example, you can use the String.format method to format numbers and dates as strings, or use the parseInt and parseDouble methods to convert strings to numbers.

To know more about String Class in java read the complete article.

String matches() method in Java.

The Java String matches() method is a static method of the java.lang.String class that is used to check if a given string matches a specified regular expression. This method returns a boolean value indicating whether the string matches the regular expression.

The String.prototype.startsWith() method determines whether a string begins with the characters of a specified string. This method returns a boolean value indicating whether the original string starts with the characters of the specified string.

The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.

Syntax:

public boolean matches(String regex)

Parameters:

regex - the regular expression to which this string is to be matched

Let’s look at a string-related Java program where the Java String startsWith() Method is used to perform an operation on the given string.

Example: Java String Matches Method

public class Main {
public static void main(String args[]) {
      String Str = new String("PrepInsta is a learning Platform");

      System.out.print("Return Value :" );
      System.out.println(Str.matches("(.*)Platform(.*)"));

      System.out.print("Return Value :" );
      System.out.println(Str.matches("Platform"));

      System.out.print("Return Value :" );
      System.out.println(Str.matches("PrepInsta(.*)"));
   }
}  

Output

true
false
false

Example 2 : Java String Matches Method

Run

public class Main {
public static void main(String args[]) {
      String str = "Hello, World!";
System.out.println(str.matches("Hello, World!"));
System.out.println(str.matches("^H.*d!$"));
System.out.println(str.matches("^h.*d!$")); 
 }
}

Output

true
true
false

Example 3: Java Spilt Method Using Regex and Length

Run
public class Main {
  public static void main(String[] args) {
    String str = "Hello, World!";

    // Check if the string matches a specific string
    System.out.println(str.matches("Hello, World!"));  // true

    // Check if the string matches a regular expression
    System.out.println(str.matches("^H.*d!$"));  // true
    System.out.println(str.matches("^h.*d!$"));  // false

    // Check if the string is a valid email address
    String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
    String email = "[email protected]";
    System.out.println(email.matches(emailRegex));  // true

    // Check if the string is a valid phone number
    String phoneRegex = "^\\(?([0-9]{3})\\)?[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4})$";
    String phone = "123-456-7890";
    System.out.println(phone.matches(phoneRegex));  // true
  }
}

Output

true
true
false
true
true

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription