如何匹配字串區域?

在Java中,如何匹配字串中的區域?

以下示例使用regionMatches()方法確定兩個字串中的區域匹配。

package com.zaixian;

public class StringRegionMatch {
    public static void main(String[] args) {
        String first_str = "Welcome to IBM";
        String second_str = "I work with IBM";

        boolean match = first_str.regionMatches(11, second_str, 12, 3);
        System.out.println("first_str[11->14] == " + "second_str[12 -> 15]: "
                + match);
    }
}

以下是幾個數字參數的說明:

  • 11 - 是比較開始的源字串中的索引號
  • second_str - 是目標字串
  • 12是從目標字串開始比較的索引號
  • 3是要比較的字元數

執行上面示例代碼,得到以下結果 -

first_str[11->14] == second_str[12 -> 15]: true

上一篇: Java字串 下一篇: Java數組