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.
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)); } }
Given String = Tutor Joe's Computer Educations Specified Sequence of char Values = Computer true Specified Sequence of char Values = Computers false
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions