Home  >  Article  >  Java  >  Java Example - Test if two string ranges are equal

Java Example - Test if two string ranges are equal

黄舟
黄舟Original
2017-02-22 09:45:131315browse

The following example uses the regionMatches() method to test whether 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) means changing the first_str string from the 11th The comparison begins with the character "M" and the 12th character "M" of the second_str string one by one. A total of 9 pairs of characters are compared. Since the string is case-sensitive, the result is false.

If the first parameter is set to true, it means that the case difference is ignored, so true is returned.

The output result of the above code example is:

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

The above is the Java example - testing whether two string areas are equal. For more related content, please pay attention to the PHP Chinese website (www.php. cn)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn