Java Program to Check if Two Words Are Present in a StringIn the realm of software development, text processing is a common task. Whether we are building a search engine, a chatbot, or any application that deals with text, we might need to determine if certain words are present in a string. In this section, we will discuss how to check if two specific words are present in a string. Java provides a rich set of methods for string manipulation through the String class. Before diving into the specifics of checking for two words, it's essential to understand some fundamental operations on strings. Here are some common methods used in string manipulation:
Checking for Words in a StringTo check if two specific words are present in a string, we can use various approaches depending on the complexity of our requirements. We will explore several methods, starting from basic to more advanced techniques. Basic Approach: Using contains() MethodThe simplest way to check if two words are present in a string is to use the contains() method. This method checks if a sequence of characters is present in the string. File Name: WordChecker.java Output: Are both words present? true In this example, the areWordsPresent() method returns true if both word1 and word2 are found in the string str. Case-Insensitive SearchStrings in Java are case-sensitive by default. To perform a case-insensitive search, we can convert both the string and the words to lowercase or uppercase before checking. File Name: CaseInsensitiveWordChecker.java Output: Are both words present (case-insensitive)? True Checking for Whole WordsThe contains() method checks for a sequence of characters, which might be part of another word. To ensure we are checking for whole words, we can use regular expressions. File Name: WholeWordChecker.java Output: Are both whole words present? true In this example, the \\b is a word boundary in the regular expression, ensuring that word1 and word2 are matched as whole words. Handling Word Variations with Regular ExpressionsRegular expressions can also handle variations of the words, such as different tenses or plural forms. For instance, to check for both "quick" and "quickly", we can use a pattern like quick(ly)?. File Name: WordVariationChecker.java Output: Are both word variations present? true In this example, word1Pattern and word2Pattern use regular expression patterns to match variations of quick and lazy. The quick(ly)? pattern matches both quick and quickly, and the laz(y|ies)? pattern matches lazy and lazies. Advanced ApproachesFor more complex scenarios, such as checking for words in large texts or in the presence of punctuation, additional strategies can be applied. Using String TokenizationString tokenization involves splitting the string into individual words and then checking if the desired words are present in the resulting array. It can be useful when dealing with punctuation and other delimiters. File Name: TokenizationChecker.java Output: Are both words present using tokenization? true Using a Set for Faster LookupsIf we need to check for the presence of words in a very large string, converting the string into a set of words can make lookups faster. File Name: SetChecker.java Output: Are both words present using a set? true Using Stream API for Modern JavaJava 8 introduced the Stream API that can be used to process sequences of elements in a functional style. Here's how we can leverage it for checking word presence: File Name: StreamChecker.java Output: Are both words present using Stream API? true Checking for the presence of two words in a string in Java can be achieved through various methods, ranging from basic to advanced techniques. Simple approaches like using contains() can be sufficient for straightforward cases, while more complex scenarios might require regular expressions, tokenization, or leveraging libraries like Apache Commons Lang. By understanding these different approaches, we can choose the most appropriate method based on your specific needs and constraints. Whether we are dealing with case sensitivity, whole word matching, or large-scale text processing, Java provides the tools and flexibility to handle these tasks efficiently. Methods ComparisonBasic Approach Using contains():
Case-Insensitive Search:
Checking for Whole Words with Regular Expressions:
Handling Word Variations with Regular Expressions:
String Tokenization:
Using a Set for Faster Lookups:
Using Apache Commons Lang:
Using Stream API for Modern Java:
|
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India