w3resource logo
Java Programming Exercies

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

Java String : Exercise-8 with Solution

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

Sample Solution:

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));
    }
}

Sample Output:

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

Java Code Editor:

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));
    }
}

Improve this sample solution and post your code through Disqus