Java.lang.String.regionMatches() 方法

描述

java.lang.String.regionMatches(int toffset, String other, int ooffset, int len) 方法测试两个字符串区域是否相等。将此 String 对象的子字符串与参数 other 的子字符串进行比较。

如果这些子字符串表示相同的字符序列,则结果为真。 要比较的此 String 对象的子字符串从索引 toffset 开始,长度为 len。 要比较的 other 的子字符串从索引 ooffset 开始,长度为 len


声明

以下是 java.lang.String.regionMatches() 方法的声明。

public boolean regionMatches(int toffset, String other, int ooffset, int len)

参数

  • toffset − 此字符串中子区域的起始偏移量。

  • other − 字符串参数。

  • ooffset − 字符串参数中子区域的起始偏移量。

  • len − 要比较的字符数。


返回值

如果此字符串的指定子区域与字符串参数的指定子区域完全匹配,则此方法返回 true,否则返回 false。


异常

NA


示例

下面的例子展示了 java.lang.String.regionMatches() 方法的使用。

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

   public static void main(String[] args) {

      String str1 = "Collection of tutorials";
      String str2 = "Consists of different tutorials";

      /* matches characters from index 14 in str1 to characters from
         index 22 in str2 considering same case of the letters.*/
      boolean match1 = str1.regionMatches(14, str2, 22, 9);
      System.out.println("region matched = " + match1);
    
      // considering different case, will return false
      str2 = "Consists of different Tutorials";
      match1 = str1.regionMatches(14, str2, 22, 9); 
      System.out.println("region matched = " + match1);   
   }
}

让我们编译并运行上面的程序,这将产生下面的结果 −

region matched = true
region matched = false