Latest web development tutorials

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 --字符串參數。

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

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

返回值

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

實例

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

		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類