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.