Java regionMatches() 方法

Java String類Java String類


regionMatches() 方法用於檢測兩個字串在一個區域內是否相等。

語法

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

或


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

參數

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

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

  • other -- 字串參數。

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

  • len -- 要比較的字元數。

返回值

如果字串的指定子區域匹配字串參數的指定子區域,則返回 true;否則返回 false。是否完全匹配或考慮大小寫取決於 ignoreCase 參數。

實例

public class Test {
    public static void main(String args[]) {
        String Str1 = new String("www.xuhuhu.com");
        String Str2 = new String("zaixian");
        String Str3 = new String("zaixian");

        System.out.print("返回值 :" );
        System.out.println(Str1.regionMatches(4, Str2, 0, 5));

        System.out.print("返回值 :" );
        System.out.println(Str1.regionMatches(4, Str3, 0, 5));

        System.out.print("返回值 :" );
        System.out.println(Str1.regionMatches(true, 4, Str3, 0, 5));
    }
}

以上程式執行結果為:

返回值 :true
返回值 :false
返回值 :true

Java String類Java String類