當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java String matches()用法及代碼示例


描述

此方法告訴此字符串是否與給定的正則表達式匹配。以 str.matches(regex) 形式調用此方法會產生與表達式 Pattern.matches(regex, str) 完全相同的結果。

用法

這是此方法的語法 -

public boolean matches(String regex)

參數

這是參數的詳細信息 -

  • regex- 與此字符串匹配的正則表達式。

返回值

  • 當且僅當此字符串與給定的正則表達式匹配時,此方法才返回 true。

示例

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome to Tutorialspoint.com");

      System.out.print("Return Value:" );
      System.out.println(Str.matches("(.*)Tutorials(.*)"));

      System.out.print("Return Value:" );
      System.out.println(Str.matches("Tutorials"));

      System.out.print("Return Value:" );
      System.out.println(Str.matches("Welcome(.*)"));
   }
}

這將產生以下結果 -

輸出

Return Value:true
Return Value:false
Return Value:true

相關用法


注:本文由純淨天空篩選整理自 Java - String matches() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。