String matches() method in Java

The boolean matches(String regex) method in Java tells whether or not this string matches the given regular expression.

Syntax

Let us see the syntax,

boolean matches(String regex)

Parameters

Let us see the parameters,

  • regex − the regular expression with which the string is matched

Example

The following is an example of matches(String regex),

public class StudyopediaDemo {

   public static void main(String args[]) {
	 
      String message = new String("Studyopedia provides free learning content!");
      System.out.println("String: "+message);
	  
      // True
      System.out.println(message.matches("Studyopedia(.*)"));
	  
      // False
      System.out.println(message.matches("(.*)Studyopedia"));
  } 
}

Output

The following is the output,

Java String matches method

String length() method in Java
String regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) method in Java
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment