Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 1.75 KB

File metadata and controls

50 lines (36 loc) · 1.75 KB

Java - String regionMatches()方法

原文: https://beginnersbook.com/2013/12/java-string-regionmatches-method-example/

方法regionMatches()测试两个字符串是否相等。使用此方法,我们可以将输入String的子字符串与指定String的子字符串进行比较。

两种变体: 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 - 要比较的字符数。

示例:regionMatches()方法

public class RegionMatchesExample{
   public static void main(String args[]){
       String str1 = new String("Hello, How are you");
       String str2 = new String("How");
       String str3 = new String("HOW");

       System.out.print("Result of Test1: " );
       System.out.println(str1.regionMatches(7, str2, 0, 3));

       System.out.print("Result of Test2: " );
       System.out.println(str1.regionMatches(7, str3, 0, 3));

       System.out.print("Result of Test3: " );
       System.out.println(str1.regionMatches(true, 7, str3, 0, 3));
   }
}

输出:

Result of Test1: true
Result of Test2: false
Result of Test3: true

参考:

[regionMatches(int,java.lang.String,int,int)](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#regionMatches(int, java.lang.String, int, int))