Java regionMatches()方法

regionMatches()方法

Java中String类内置的regionMatches()方法,可用于判断两个字符串在一个索引区间内的子串是否相等(可根据需要通过参数设置是否忽略大小写)。regionMatches()语法如下:

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

regionMatches()参数

参数描述
ignoreCase可选参数,用于设置是否忽略大小写,true为忽略,false则为不忽略
toffset调用对象字符串中索引的起始偏移量,也就是开始比较的起始索引位置
other用于与调用对象进行比较的字符串
ooffsetother参数指定的字符串的起始偏移量,也就是开始比较的索引位置
len设定用于比较的字符数

regionMatches()返回值

boolean类型值,如果特定索引区间内的子串相等,则为true,否则为false。

regionMatches()实例代码

public class test {
    public static void main(String[] args) {
        String str1 = "Hello,全栈开发助手";
        String str2 = "全栈开发助手";
        boolean t1 = str1.regionMatches(6, str2, 0, 2);
        boolean t2 = str1.regionMatches(5, str2, 0, 2);
        System.out.println("t1="+t1+"; t2="+t2);
    }

}

代码运行后,得到输出值为:

t1=true; t2=false

免责声明:内容仅供参考,不保证正确性。


全栈后端 / Java教程 :









Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.