Java.lang.String.regionMatches() 方法



描述

java.lang.String.regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 方法測試兩個字串區域是否相等。將此 String 物件的子字串與引數 other 的子字串進行比較。

如果這些子字串表示相同的字元序列,則結果為 true,當且僅當 ignoreCase 為 true 時忽略大小寫。要比較的此 String 物件的子字串從索引 toffset 開始,長度為 len。要比較的 other 的子字串從索引 ooffset 開始,長度為 len

宣告

以下是 java.lang.String.regionMatches() 方法的宣告

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

引數

  • ignoreCase - 如果為 true,則在比較字元時忽略大小寫。

  • toffset - 此字串中子區域的起始偏移量。

  • other - 字串引數。

  • ooffset - 字串引數中子區域的起始偏移量。

  • len - 要比較的字元數。

返回值

如果此字串的指定子區域與字串引數的指定子區域匹配,則此方法返回 true,否則返回 false。

異常

示例

以下示例顯示了 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, "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
java_lang_string.htm
廣告

© . All rights reserved.