The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. Show
The String class has a number of methods for comparing strings and portions of strings. The following table lists these methods.
The following program, RegionMatchesDemo, uses the regionMatches method to search for a string within another string: public class RegionMatchesDemo { public static void main(String[] args) { String searchMe = "Green Eggs and Ham"; String findMe = "Eggs"; int searchMeLength = searchMe.length(); int findMeLength = findMe.length(); boolean foundIt = false; for (int i = 0; i <= (searchMeLength - findMeLength); i++) { if (searchMe.regionMatches(i, findMe, 0, findMeLength)) { foundIt = true; System.out.println(searchMe.substring(i, i + findMeLength)); break; } } if (!foundIt) System.out.println("No match found."); } } The output from this program is Eggs. The program steps through the string referred to by searchMe one character at a time. For each character, the program calls the regionMatches method to determine whether the substring beginning with the current character matches the string the program is looking for. This method has two variants which can be used to test if two string regions are equal. SyntaxHere is the syntax of this method − public boolean regionMatches(int toffset, String other, int ooffset, int len)ParametersHere is the detail of parameters −
Return Value
Exampleimport java.io.*; public class Test { public static void main(String args[]) { String Str1 = new String("Welcome to Tutorialspoint.com"); String Str2 = new String("Tutorials"); String Str3 = new String("TUTORIALS"); System.out.print("Return Value :" ); System.out.println(Str1.regionMatches(11, Str2, 0, 9)); System.out.print("Return Value :" ); System.out.println(Str1.regionMatches(11, Str3, 0, 9)); } }This will produce the following result − OutputReturn Value :true Return Value :falsejava_strings.htm
DownloadApp
Home » JAVA Programming » Strings » Question
regionMatches()
Improve Article Save Article Like Article The regionMatches() method of the String class has two variants that can be used to test if two string regions are matching or equal. There are two variants of this method, i.e., one is case sensitive test method, and the other ignores the case-sensitive method. Syntax: 1. Case sensitive test method: public boolean regionMatches(int toffset, String other, int offset, int len)2. It has the option to consider or ignore the case method: public boolean regionMatches(boolean ignoreCase, int toffset, String other, int offset, int len)Parameters:
Return Value: A substring of the String object is compared to a substring of the argument other. The result is true if these substrings represent character sequences that are the same, ignoring case if and only if ignoreCase is true. The substring of this String object to be compared begins at index toffset and has length len. The substring of other to be compared begins at index offset and has length len. The result is false if and only if at least one of the following is true Example 1:
OutputResult of Comparing of String 1 and String 2: true Result of Comparing of String 1 and String 3: false Result of Comparing of String 2 and String 3: false Example 2:
OutputResult of comparing String 1 and String 2 : true Result of comparing String 1 and String 3 : false Result of comparing String 2 and String 3 : true Note: The method returns false if at least one of these is true,
|