Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java String Exercises: Check if a given string contains another string

Java String: Exercise-100 with Solution

Write a Java program to check if a given string contains another string. Return true or false.

Sample Solution:

Java Code:

public class Main {
    
    public static void main(String[] args) {
          String main_string = "Java is the foundation for virtually every type of "+
           "networked application and is the global standard for developing and "+
           " delivering embedded and mobile applications, games, Web-based content, "+
           " and enterprise software. With more than 9 million developers worldwide,"+
           " Java enables you to efficiently develop, deploy and use exciting applications and services.";
            System.out.println("Original string:");
           System.out.println(main_string);
           String sub_string1 = "million";
            String sub_string2 = "millions";
           boolean result1 = is_present(main_string, sub_string1);
            System.out.println("\nIs '"+sub_string1+"'"+ " present in the said text?");
           System.out.println(result1);
           boolean result2 = is_present(main_string, sub_string2);
            System.out.println("\nIs '"+sub_string2+"'"+ " present in the said text?");
           System.out.println(result2);                        
 }
    public static boolean is_present(String main_string, String sub_string) {
      if (main_string == null || sub_string == null || main_string.isEmpty() || sub_string.isEmpty()) {
            return false;
       }
       return main_string.indexOf(sub_string) != -1;
   }
}

Sample Output:

Original string:
Java is the foundation for virtually every type of networked application and is the global standard for developing and  delivering embedded and mobile applications, games, Web-based content,  and enterprise software. With more than 9 million developers worldwide, Java enables you to efficiently develop, deploy and use exciting applications and services.

Is 'million' present in the said text?
true

Is 'millions' present in the said text?
false

Pictorial Presentation:

Java String Exercises: Check if a given string contains another string
Java String Exercises: Check if a given string contains another string

Flowchart:

Flowchart: Java String Exercises - Return a new string using every characters of even positions from a given string

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to return a new string using every character of even positions from a given string.
Next: Write a Java program to test if a given string contains only digits. Return true or false.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe