Java 示例 - 字符串区域匹配

  • 问题描述

    如何匹配字符串中的区域?
  • 解决方案

    以下示例使用 regionMatches() 方法确定两个字符串中的区域匹配。
    
    public class StringRegionMatch {
       public static void main(String[] args) {
          String first_str = "Welcome to Microsoft";
          String second_str = "I work with Microsoft";
          boolean match = first_str.regionMatches(11, second_str, 12, 9);
          System.out.println("first_str[11 -19] == " + "second_str[12 - 21]:-"+ match);
       }
    }
    
    • 11 是源字符串中比较开始的索引号
    • Second_str 是目标字符串
    • 12 是目标字符串中比较应该从哪里开始的索引号
    • 9 是要比较的字符数
  • 结果

    上面的代码示例将产生以下结果。
    
    first_str[11 -19] == second_str[12 - 21]:-true