Sometimes we need to match some words in a particular String. We can do this easily by matches() method of String class.
Example:
package com.demo;
public class TestApps {
/*
*@param args
*/
public static void main(String[] args){
String Str = new String("You are welcome to Findnerd.com");
//Matches word in between
System.out.print("Value :" );
System.out.println(Str.matches("(.*)Findnerd(.*)"));
System.out.print("Value :" );
System.out.println(Str.matches("Findnerd"));
//Matches whether the word is last word
System.out.print("Value :" );
System.out.println(Str.matches("(.*)com"));
//Matches whether the word is first word
System.out.print("Value :" );
System.out.println(Str.matches("You(.*)"));
}
}
Output:
Value :true
Value :false
Value :true
Value :true
Hope this will help you :)
0 Comment(s)