java tutorial - Java Strings matches() method - java programming - learn java - java basics - java for beginners



Java strings matches

Learn Java - Java tutorial - Java strings matches - Java examples - Java programs

Description

The matches () method — in Java, tells whether or not the given string matches a given regular expression. Calling this method from the str.matches (regex) form produces exactly the same result as the expression Pattern.matches (regex, str).

Syntax

Syntax method:

public boolean matches(String regex)
click below button to copy the code. By - java tutorial - team

Options

Detailed information about the parameters:

  • regex is a regular expression to which this string must match.

Return value

  • In Java matches (), returns true if, and only if this string matches a specified regular expression.

Sample Code

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome to wikitechy.com");

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

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

      System.out.print("Return Value :" );
      System.out.println(Str.matches("Welcome(.*)"));
   }
}
click below button to copy the code. By - java tutorial - team

Output

Return Value :true
Return Value :false
Return Value :true

Related Searches to Java Strings matches() method