Java String regionMatches()方法(忽略大小寫)

Java String regionMatches()方法有兩個變體,可用於測試兩個字串區域是否相等。

語法

以下是此方法的語法 -

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

參數

  • ignoreCase - 指示是否忽略大小寫。
  • toffset - 此字串中子區域的起始偏移量。
  • other - 字串參數。
  • offset - 字串參數中子區域的起始偏移量。
  • len - 要比較的字元數。

返回值

如果此字串的指定子區域與字串參數的指定子區域匹配,則返回true;否則返回false。匹配是精確匹配還是不區分大小寫取決於ignoreCase參數。

示例

import java.io.*;

public class Test {

    public static void main(String args[]) {
        String Str1 = new String("Welcome to xuhuhu.com");
        String Str2 = new String("zaixian");

        System.out.print("Return Value(ignoreCase=true):");
        System.out.println(Str1.regionMatches(true, 11, Str2, 0, 6));
        System.out.print("Return Value(ignoreCase=false):");
        System.out.println(Str1.regionMatches(false, 11, Str2, 0, 6));
    }
}

執行上面示例代碼,得到以下結果:

Return Value(ignoreCase=true):true
Return Value(ignoreCase=false):false

上一篇: java中方法重載和方法重寫的區別 下一篇:無