Latest web development tutorials

Java Examples - Tests if two string regions are equal

Java Examples Java Examples

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

//StringRegionMatch.java 文件

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); //第一个参数 true 表示忽略大小写区别
      System.out.println("区分大小写返回值:" + match1);
      System.out.println("不区分大小写返回值:" + match2);
   }
}

first_str.regionMatches (11, second_str, 12, 9) said it would first_str string from the first 11 characters "M" and the beginning of the first 12 characters of the string second_str "M" began one by comparing total of nine pairs of characters, because the characters string is case-sensitive, so the result is false.

If the first argument is true, it means ignoring case differences, so return true.

The output is the above code examples:

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

Java Examples Java Examples