Java regionMatches() メソッド
regionMatches() メソッドは、2つの文字列が1つの領域内で等しいかどうかを検出するために使用されます。
構文
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.example.com");
String Str2 = new String("サンプル");
String Str3 = new String("サンプル");
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));
}
}
}
public static void main(String args[]) {
String Str1 = new String("www.example.com");
String Str2 = new String("サンプル");
String Str3 = new String("サンプル");
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クラス