String Matches in Java
Firstly we have to know something about String?
Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language.
“String” is a Java platform class that allows you to construct and handle strings.
Now take a look that how we Creating Strings
The normal way to create a string in Java is to simply write:-
String s = “Hello World”;
The compiler constructs a String object with the value "Hello world!" whenever it detects a string phrase in your code.
String objects cannot be made by using the new keyword and a function Object, just like any other object. The String class provides 11 constructors that allow you to set the string's initial value from a variety of sources, including an array of characters.
Another way to create a string is:-
String s = new String (“Hello World”);
Now let’s come to the main topic.
String matches :
Variants of the matches() method are being used to check whether a given text matches a regular expression or not. Regardless of whether this method is called matches() or matches(), where we pass two arguments, our string and our regular expression, the working and output stay the same.
There are three variations of the matches() methods, which are mentioned and explained below:
String matches()
This method determines whether the provided string satisfies the regular expression. The expression Pattern.matches gets the very same result as invoking this function with the form str.matches(regex).
Syntax:
public boolean matches(String regex)
Return Type:
Boolean Value, If and only if strings satisfy the specified regular expression, this value is true; otherwise, it is false.
Parameter;
String is to be matched in a regular expression.
Take a look on an Example:
// Program to Illustrate the operation of Matches() Method
// Main class
public class GFG {
public static void main(String args[]){
// Declaring and initializing a string
String Str = new String("Welcome to Java");
System.out.print(
"Does String contains regex (.*)java(.*) ? : ");
// Analising if regex is present or not
System.out.println(Str.matches("(.*)java(.*)"));
System.out.print(
"Does String contains regex java ? : ");
// Analising if regex is present or not
System.out.println(Str.matches("java"));
}
}
OUTPUT:
Does String contains regex (.*)java(.*) ? : true
Does String contains regex java ? : false
String regionMatches()
Variants of regionMatches() method
RegionMatches(int toffset, other, int offset, int len)
RegionMatches(int toffset, other, int offset, int len, ignore case)
Parameters:
regionMatches
- other: the argument of string.
- Int offset: the starting offset of the subregion in the string argument.
- int toffset: the starting offset of the subregion in this string.
- Int len: different types of characters for comparison.
Return Value:
This function returns true if the given subregion of this string satisfies the specified subregion of the string argument; else, it returns false. The ignoreCase option determines whether the matching is accurate or case insensitive.
Take a look on an Example:
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str1 = new String("Welcome to my World");
String Str2 = new String("java");
String Str3 = new String("JAVA");
System.out.print("Return Value :" );
System.out.println(Str1.regionMatches(11, Str2, 0, 4));
System.out.print("Return Value :" );
System.out.println(Str1.regionMatches(11, Str3, 0, 4));
}
}
OUTPUT:
Return Value : true
Return Value : False
Syntax:
public boolean regionMatches(int str_strt, String other, int other_strt,int len)
String regionMatches() With ignoreCase
This approach offers two variations for determining whether two string segments are equal.
Parameters:
- Int toffset: the starting offset of the subregion in this string.
- other: the argument of string.
- Int offset: the starting offset of the subregion in the string argument.
- Int len: different types of characters for comparison.
- ignoreCase – if it is true, ignore the case when comparing with characters.
Return Value:
If this string's stated subregion satisfies the string argument's specified subregion, it gives true; otherwise, it gives false. The ignoreCase argument determines whether the matching is exact or case-insensitive.
Take a look at an example:
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str1 = new String("Welcome to my world");
String Str2 = new String("HELLOWORLD");
System.out.print("Return Value :" );
System.out.println(Str1.regionMatches(true, 11, Str2, 0, 10));
}
}
Output:
Return value : True
Syntax:
public boolean regionMatches(boolean ignoreCase,
int toffset, offset, String other,int,int len)