java.lang.String.regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)方法實例
java.lang.String.regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 測試方法如果兩個字符串的區域都是相等。這個String對象的子字符串進行比較其他的子串參數。
其結果是true 如果這些子表示是相同的,忽略大小寫當且僅當IGNORECASE是真實的字符序列。這個String對象進行比較的字符串開始處indextoffset和長度爲len。其他的子字符串進行比較始於索引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。
異常
- NA
例子
下面的例子顯示java.lang.String.regionMatches()方法的使用。
package com.yiibai; 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