String class matches method example:- This example demonstrates the working of matches() method. this method returns boolean values if it gets same match of object value to the passes argument value in the parentheses.
Syntax:-matches(String regex)
Here is the code:-
/**Output of the program:-
* @(#) MatchesString.java
* MatchesString class demonstrates the working of matches() method of String class of lang package
* @version 16-May-2008
* @author Rose India Team
*/
public class MatchesString {
public static void main(String args[]) {
// method just matches the String value to the specified value in the
// parentheses and generates boolean values
String str = " heram", str1 = "xaaaaa";
boolean result = str1.matches("aaaaa");
// method not get the same values therefore it returns false
System.out.println("Method returns 'false': " + result);
// method gets the same values but not able to match there allignments
// therefore it returns false
result = str.matches("heram");
System.out.println("Method returns 'false': " + result);
// method gets the same values with precise allignments therefore it
// returns true
result = str.matches(" heram");
System.out.println("Method returns 'true': " + result);
}
}
Method returns 'false': false Method returns 'false': false Method returns 'true': true |
No comments:
Post a Comment