java tutorial - Java string contains - java programming - learn java - java basics - java for beginners



 Java String Contains

Learn java - java tutorial - Java String Contains - java examples - java programs

Java String contains :

  • This method searches the sequence of characters in this string. If the sequence of char values is found in this string, then it returns true or else it returns false.

Parameter :

  • sequence : it specifies the sequence of characters to be searched.

Returns :

  • True if the sequence of char value exists, or else false.

Throws :

  • NullPointerException : if the sequence is null.

Syntax :

  • The Java String class has the method contains() to check if a String contains a specified character sequence. The syntax is shown below:
public boolean contains(CharSequence s);

Implementation:

public boolean contains(CharSequence s) {
        return indexOf(s.toString()) > -1;
    }

Sample Code:

public class kaashiv_infotech
{
    public static void main(String args[])
    {
        String str1 = "kaashiv_infotech";
        String str2 = "kaashiv";
 
        boolean valueFound = str1.contains(str2);
        System.out.println("String contains another string? : " + valueFound);
 
    }
}

Output:

String contains another string? : true

Related Searches to Java string contains