Monday, 7 May 2012

Java String matches Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
String class matches method example

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:-
/**
 * @(#) MatchesString.java 
 * MatchesString class demonstrates the working of matches() method of String class of lang package
 * 
 */


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);
  }
}
Output of the program:-
------------------------------
Method returns 'false':  false
Method returns 'false':  false
Method returns 'true':  true

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...