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


This Java program demonstrates how to check if a string contains a specified sequence of characters using the contains() method of the String class.

In this program, a string "Tutor Joe's Computer Educations" is assigned to the str1 variable. Two more strings "Computer" and "Computers" are assigned to str2 and str3 variables respectively.

Then, the contains() method is called on str1 with str2 as an argument, and the result is printed using the println() method. Similarly, the contains() method is called on str1 with str3 as an argument, and the result is printed.

The contains() method returns true if the specified sequence of characters is present in the given string, otherwise it returns false.

Source Code

public class String_Contains
{
	public static void main(String[] args)
	{
		String str1 = "Tutor Joe's Computer Educations";
		String str2 = "Computer";
		String str3 = "Computers";
		System.out.println("Given String : " + str1);
		System.out.println("Specified Sequence of char Values : " + str2);
		System.out.println(str1.contains(str2));
		System.out.println("\nSpecified Sequence of char Values : " + str3);
		System.out.println(str1.contains(str3));
	}
}

Output

Given String = Tutor Joe's Computer Educations
Specified Sequence of char Values = Computer
true

Specified Sequence of char Values = Computers
false

Example Programs