ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Java - `String regionMatches()`方法 > 原文: [https://beginnersbook.com/2013/12/java-string-regionmatches-method-example/](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()`方法 ```java 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)); } } ``` 输出: ```java 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))