Java - String regionMatches() 方法



Java String regionMatches() 方法检查两个字符串区域是否等效。它将当前对象的子字符串与给定字符串的子字符串进行比较。

如果这些子字符串表示相同的字符序列,则此方法返回 true。此方法有两个多态变体。它们的语法如下所示,一个区分大小写,另一个忽略大小写。

语法

以下是Java String regionMatches() 方法的语法:

public boolean regionMatches(int toffset, String other, int ooffset, int len) // first syntax
or,
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) // second syntax

参数

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

  • other − 字符串参数。

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

  • len − 要比较的字符数。

  • ignoreCase − 如果为 true,则在比较字符时忽略大小写。# 第二种语法

返回值

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

检查字符串区域是否等效的示例

以下示例显示了 Java String regionMatches() 方法的使用方法。这里我们初始化两个 String 对象 str1 和 str2,并匹配字符串 "str1" 中索引 "14" 处的字符与字符串 "str2" 中索引 "22" 处的字符,同时考虑字母的大小写:

package com.tutorialspoint;
 
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

忽略大小写时检查字符串区域是否等效的示例

通过初始化 String 对象并匹配字符串 "str1" 中索引 "14" 处的字符与字符串 "str2" 中索引 "22" 处的字符,同时考虑字母的大小写,以下示例显示了如何使用 Java String regionMatches() 方法:

package com.tutorialspoint;
 
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, "true" is set which will ignore
         case when matched */
      str2 = "Consists of different Tutorials";
      match1 = str1.regionMatches(true, 14, str2, 22, 9); 
      System.out.println("region matched = " + match1);   
   }
}

输出

如果编译并运行上述程序,输出将如下所示:

region matched = true
region matched = true

示例

下面是另一个 regionMatches() 方法的代码,其中取一个字符串 "Welcome to Tutorialspoint",并将其子字符串视为 "TUTORIALS" 和 "Tutorials"。s1 的索引大于字符串长度,s2 的索引在字符串长度内:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String args[]) {
      String s1 = new String("Welcome to Tutorialspoint");
      String s2 = new String("TUTORIALS");
      String s3 = new String("Tutorials");
      System.out.print("Return Value :");
      System.out.println(s1.regionMatches(50, s2, 0, 9)); 
      System.out.print("Return Value :");
      System.out.println(s1.regionMatches(11, s3, 0, 9));
   }
}

输出

运行上述程序后,输出结果如下:

Return Value :false
Return Value :true

使用负索引检查字符串区域是否等效的示例

在以下示例中,regionMatches() 方法用于查找给定两个字符串的子字符串或区域是否相等,同时传递负索引:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String args[]) {
      String s1 = new String("Welcome to TutorixProject");
      String s2 = new String("TUTORIX");
      String s3 = new String("Tutorix");
      System.out.print("Return Value :");
      System.out.println(s1.regionMatches(-11, s2, 0, 9));
      System.out.print("Return Value :");
      System.out.println(s1.regionMatches(-11, s3, 0, 9));
   }
}

输出

上述程序的输出如下:

Return Value :false
Return Value :false
java_lang_string.htm
广告
© . All rights reserved.