Java Examples - Test whether two string regions are equal

Java 实例Java Examples

The following example uses the regionMatches() method to test whether two string regions are equal:

StringRegionMatch.java file

public class StringRegionMatch{ public static void main(String[] args){ String first_str = "Welcome to Microsoft"; String second_str = "I work with microsoft"; boolean match1 = first_str. regionMatches(11, second_str, 12, 9); boolean match2 = first_str. regionMatches(true, 11, second_str, 12, 9); //The first parameter true means ignoring case differences System.out.println("Case-sensitive return value:" + match1); System.out.println("Case-insensitive return value:" + match2); } }

first_str.regionMatches(11, second_str, 12, 9)Indicates that thefirst_str string starting from the 11th character 'M' andsecond_strthe string's 12th character 'M' are compared one by one, comparing 9 pairs of characters in total. Since the strings are case-sensitive, the result is false.

If the first parameter is set to true, it means case differences are ignored, so it returns true.

The output result of the above code example is:

区分大小写返回值:false 
不区分大小写返回值:true

Java 实例Java Examples

Other extensions