w3resource logo


Java Programming Exercies

Java String Exercises: Test if a given string contains the specified sequence of char values

Secondary Nav

Java String : Exercise-8 with Solution

Write a Java program to test if a given string contains the specified sequence of char values.

Java Code:

public class Exercise8 {

    public static void main(String[] args)
    {
        String str1 = "PHP Exercises and Python Exercises";
        String str2 = "and";
        System.out.println("Original String: " + str1);
        System.out.println("Specified sequence of char values: " + str2);
        System.out.println(str1.contains(str2));
    }
}

Output:

Original String: PHP Exercises and Python Exercises                     
Specified sequence of char values: and                                  
true

Java Practice online:

public class Exercise8 {

    public static void main(String[] args)
    {
        String str1 = "PHP Exercises and Python Exercises";
        String str2 = "and";
        System.out.println("Original String: " + str1);
        System.out.println("Specified sequence of char values: " + str2);
        System.out.println(str1.contains(str2));
    }
}



Join our Question Answer community to learn and share your programming knowledge.