News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Java String contains Example

Started by VelMurugan, Nov 26, 2008, 06:29 PM

Previous topic - Next topic

VelMurugan

Java String contains Example

String class contains method example

String class contains method example. This example shows you how to use contains method.This method  Returns true if and only if this string contains the specified sequence of char values.
Syntax:-contains(CharSequence s)

Here is the code:-

/**
* @(#) ContainsString.java
*Program that Returns true if and only if this string contains the specified sequence of char values.
* @Version  01-May-2008
* @author   Rose India Team
*/
class ContainsString
  {
    public static void main(String[] args)
  {
    //Declaration of first string
        String str1 = "Welcome to RoseIndia";
    //Returns true if and only if this string contains the specified sequence of char values.
    boolean equals1 = str1.contains("Welcome");
     System.out.println(equals1);
     }
}


Source : CodeDiary

VelMurugan

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:-

QuoteMethod returns true here:  'true'

'true' is returned:    'true'

'false' is returned:    'false'