Search Java Programs

Wednesday, January 20, 2010

Java String contains Example

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
* @version 14-May-2008
* @author Rose India Team
*/



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

Website Design by Mayuri Multimedia