當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.lang.String.regionMatches()用法及代碼示例


描述

這個java.lang.String.regionMatches(int toffset, String other, int ooffset, int len) 方法測試兩個字符串區域是否相等。將此 String 對象的子字符串與參數的子字符串進行比較other

如果這些子串表示相同的字符序列,則結果為真。要比較的此 String 對象的子字符串從 index 開始toffset並且有長度len.要比較的 other 的子串從 index 開始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

相關用法


注:本文由純淨天空篩選整理自 Java.lang.String.regionMatches() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。