Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Java String matches Method

by suresh

The Java String.matches method is a Java String Method, which tells whether the string matches the user-specified regular expression or not. Based on the result, it will return Boolean True or False.

The basic syntax of the string matches in Java Programming language is as shown below.

The following method will accept the Regular expression as the argument and check whether the original string matches this expression or not.

public boolean matches(String regexp); // It will return boolean True or False 

//In order to use in program
String_Object.matches(String regexp);

Java String matches Example

In this program, we use the String.matches method to check whether this string is exactly matching with the user-specified expression or not.

// Java String.matches example
package StringFunctions;

public class MatchMethod {
	public static void main(String[] args) {
		String str = "Learn Java at Tutorial Gateway";
		String str1 = "We are abc working at abc Company";
		String str2 = "We are abc working at abc Company";
		
		boolean bool1 = str1.matches("abc");
		boolean bool2 = str1.matches(str2);		
		boolean bool3 = str.matches("Learn Java(.*)");
		boolean bool4 = str.matches("(.*)Java(.*)");

		System.out.println("Does String str1 Matches with 'abc'? = " + bool1);
		System.out.println("Does String str1 Matches with str2? = " + bool2);
		System.out.println("Does String str1 Matches with RegExp? = " + bool3);
		System.out.println("Does String str1 Matches with RegExp? = " + bool4);
	}
}

OUTPUT

Java String Matches Method 1

ANALYSIS

The following statement will call the public boolean matches (String regex) method to check whether the string str1 matches with “abc” or not. From the above screenshot, you can observe that it is returning False because they are not matching.

boolean bool1 = str1.matches("abc");

It will check whether the string str matches with “Learn Java (.*)” or not. It means the string should start with Learn Java, and it will accept anything after that.

boolean bool3 = str.matches("Learn Java(.*)");

The following statement will check whether the string str matches with “(.*)Java (.*)” or not. It means the string must contain Java, and it will accept anything before or after the java word.

boolean bool4 = str.matches("(.*)Java(.*)");

Lastly, we used the Java System.out.println statements will print the output.

Java String matches Example

In this Java program, We are going to ask the user to enter any word. Based on the user entered string value, this String Function will display the message.

package StringFunctions;
import java.util.Scanner;

public class MatchMethodex {
	private static Scanner sc;
	public static void main(String[] args) {
		sc = new Scanner(System.in);		
		System.out.println("Please Enter any word: ");
		String str = sc.nextLine();
		
		if (str.matches("(.*)java(.*)")) {
			System.out.println("Hey!! Welcome to Java Programming Language");
		}
		else {
			System.out.println("Goodbye Tutorial Gateway");
		}
	}
}

OUTPUT

Java String Matches Method 2

Let us enter different word

Java String Matches Method 3

ANALYSIS

Within this String matches example, the following statement will ask the user to enter any word. Then we are going to assign the user entered value to string variable str.

System.out.println("Please Enter any word: ");
String str = sc.nextLine();

Next, we used the If Else Statement to check whether the user entered string matches with “(.*)Java (.*)” or not. It means the string must contain Java, and it will accept anything before or after the java word.

  • If the statement inside the If Statement is True, System.out.println(“Hey!! Welcome to Java Programming Language”); statement printed.
  • Else System.out.println(“Goodbye to Java Tutorials”); statement printed.

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy