📜  如何在Java中匹配字符串中的区域?

📅  最后修改于: 2022-05-13 01:54:25.961000             🧑  作者: Mango

如何在Java中匹配字符串中的区域?

regionMatches()方法有两个变体,可用于测试两个字符串区域是否匹配或相等。

句法:

区分大小写的测试方法:

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

它可以选择考虑或忽略 case 方法:

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

参数:

  • ignoreCase :如果为 true,则在比较字符时忽略大小写。
  • tooffset:该字符串中子区域的起始偏移量。
  • other :正在比较的字符串参数。
  • ooffset :字符串参数中子区域的起始偏移量。
  • len :要比较的字符数。

返回值:

如果此字符串的指定子区域与字符串参数的指定子区域匹配,则返回 true,否则返回 false。如果ignoreCase为 true,则它返回 true 或 false 不区分大小写。

示例 1:

Java
// Java Program to find if substrings or regions of two
// strings are equal
  
import java.io.*;
  
class CheckIfRegionsEqual {
    public static void main(String args[])
    {
  
        // create three string objects
        String str1 = new String(
                   "Welcome to Geeksforgeeks.com");
        String str2 = new String("Geeksforgeeks");
        String str3 = new String("GEEKSFORGEEKS");
  
        // Comparing str1 and str2
        System.out.print(
            "Result of Comparing of String 1 and String 2: ");
        System.out.println(
            str1.regionMatches(11, str2, 0, 13));
  
        // Comparing str1 and str3
        System.out.print(
            "Result of Comparing of String 1 and String 3: ");
        System.out.println(
            str1.regionMatches(11, str3, 0, 13));
  
        // Comparing str2 and str3
        System.out.print(
            "Result of Comparing of String 2 and String 3: ");
        System.out.println(
            str2.regionMatches(0, str3, 0, 13));
    }
}


Java
// Java Program to find if substrings or regions of two
// strings are equal
  
import java.io.*;
  
class CheckIfRegionsEqual {
    public static void main(String args[])
    {
  
        // create three string objects
        String str1 = new String("Abhishek Rout");
        String str2 = new String("abhishek");
        String str3 = new String("ABHISHEK");
  
        // Comparing str1 and str2 substrings
        System.out.print(
            "Result of comparing String 1 and String 2 : ");
        System.out.println(
            str1.regionMatches(true, 0, str2, 0, 8));
  
        // Comparing str1 and str3 substrings
        System.out.print(
            "Result of comparing String 1 and String 3 : ");
        System.out.println(
            str1.regionMatches(false, 0, str3, 0, 8));
  
        // Comparing str2 and str3 substrings
        System.out.print(
            "Result of comparing String 2 and String 3 : ");
        System.out.println(
            str2.regionMatches(true, 0, str3, 0, 8));
    }
}


输出
Result of Comparing of String 1 and String 2: true
Result of Comparing of String 1 and String 3: false
Result of Comparing of String 2 and String 3: false


示例 2:

Java

// Java Program to find if substrings or regions of two
// strings are equal
  
import java.io.*;
  
class CheckIfRegionsEqual {
    public static void main(String args[])
    {
  
        // create three string objects
        String str1 = new String("Abhishek Rout");
        String str2 = new String("abhishek");
        String str3 = new String("ABHISHEK");
  
        // Comparing str1 and str2 substrings
        System.out.print(
            "Result of comparing String 1 and String 2 : ");
        System.out.println(
            str1.regionMatches(true, 0, str2, 0, 8));
  
        // Comparing str1 and str3 substrings
        System.out.print(
            "Result of comparing String 1 and String 3 : ");
        System.out.println(
            str1.regionMatches(false, 0, str3, 0, 8));
  
        // Comparing str2 and str3 substrings
        System.out.print(
            "Result of comparing String 2 and String 3 : ");
        System.out.println(
            str2.regionMatches(true, 0, str3, 0, 8));
    }
}
输出
Result of comparing String 1 and String 2 : true
Result of comparing String 1 and String 3 : false
Result of comparing String 2 and String 3 : true


如果至少其中之一为真,则该方法返回假,

  • 偏移量为负。
  • 偏移量为负。
  • tooffset+len 大于此 String 对象的长度。
  • ooffset+len 大于另一个参数的长度。
  • ignoreCase 为假,并且存在一些小于 len 的非负整数k使得:
  • ignoreCase 为真,并且存在一些小于 len 的非负整数k使得: