Java-string-matches

提供:Dev Guides
移動先:案内検索

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 finddevguides.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