Java regionMatches() 方法

Java String類Java String類


regionMatches() 方法用于檢測兩個字符串在一個區(qū)域內(nèi)是否相等。

語法

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

或

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

參數(shù)

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

  • toffset -- 此字符串中子區(qū)域的起始偏移量。

  • other -- 字符串參數(shù)。

  • ooffset -- 字符串參數(shù)中子區(qū)域的起始偏移量。

  • len -- 要比較的字符數(shù)。

返回值

如果字符串的指定子區(qū)域匹配字符串參數(shù)的指定子區(qū)域,則返回 true;否則返回 false。是否完全匹配或考慮大小寫取決于 ignoreCase 參數(shù)。

實(shí)例

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


		System.out.println("value:");
		System.out.println(Str1.regionMatches(4, Str2, 0, 5));


		System.out.println("value:");
		System.out.println(Str1.regionMatches(4, Str3, 0, 5));


		System.out.println("value:");
		System.out.println(Str1.regionMatches(true, 4, Str3, 0, 5));
	}
}

以上程序執(zhí)行結(jié)果為:

value:
true
value:
false
value:
true

Java String類Java String類