Monday 7 May 2012

Java String contains 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
43
44
45
46
47
48
49
50
51
52
53
String class Contains method example

String class Contains method example:- 
This example demonstrates the working of contains() method. 
this method generates boolean value true if and only if it finds the same sequence of data in the called String.

Syntax:-contains(CharSequence s) 


Here is the code:-

  /**
  * @(#) ContainsString.java
  * ContainsString class demonstrates the working of contains() method of String class of lang package
  *
  */  


public class ContainsString {
  public static void main(String args[]) {

    // contains() method checks the sequence characters and
    // number or both characters and numbers together passed in parentheses
    // into the called String
    String str = "om namah shivaya", str1 = "420_9211", str2, str3;

    CharSequence char0 = "am", char1 = "_";
    boolean result = str.contains(char0);

    // Method ables to find the sequence of characters(am) therefore it
    // generates boolean type true
    System.out.println("Method returns true here:  " + "'" + result + "'");
    result = str1.contains(char1);
    System.out.println();
    // Method ables to fing the symbol underscore(_) therefore it generates
    // boolean type true"
    System.out.println("'true' is returned:    " + "'" + result + "'");

    System.out.println();
    str2 = " hare ram1 hare ram, hare krishna hare ram ";
    System.out.println("'false' is returned:    " + "'"
        + str2.contains("krishna ram1") + "'");

  }

}
Output of the program:-
----------------------------------
Method returns true here:  'true'

'true' is returned:    'true'

'false' is returned:    'false'

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...