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:
Flowchart:
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
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework