
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Two Words Are Present in a String in Java
In this article, we will learn how to check if two given words are present in a string. This is useful when you need to check for multiple words in the text for different purposes, like searching, filtering, or text analysis. We will explore different approaches on how to implement it in Java, starting from the simple method to more advanced techniques.
Example Scenario
Consider this string: "Java provides powerful libraries for developers."
We want to check if both "java" and "libraries" appear in the text. In this case, the output will be true.
1.Using contains()
The contains() method checks if a string contains a specific word or substring. This is the easiest method to check for two words in a string.
Steps to Implement
The following steps outline how to check for two words using the contains() method ?
Step 1: Make a method that takes text and two words as input.
Step 2: Use contains() method to check if both words are present in the text.
Step 3: Return true if both words are present; if not, return false.
Implementation code
Below is the Java implementation of this approach ?
public class WordChecker { public static boolean bothWords(String txt, String w1, String w2) { return txt.contains(w1) && txt.contains(w2); } public static void main(String[] args) { String txt = "java provides powerful libraries for developers"; String w1 = "java"; String w2 = "libraries"; boolean res = bothWords(txt, w1, w2); System.out.println(res); // Output: true } }
Output
true
2. Case-Insensitive approach
Java is a case-sensitive language, which means it treats uppercase and lowercase letters differently. So the first method will only give the exact output if the letters are in the same case. We can overcome this limitation by initially converting the text and words to lowercase before performing the check.
Steps to Implement
The following are the steps on how we handle case sensitivity ?
Step 1: Change the text and words to lowercase.
Step 2: Use contains() to check if both words exist in the text.
Step 3: Return true if both words are found; otherwise, return false.
Implementation code
Below is the Java implementation of this case-insensitive approach ?
public class WordChecker { public static boolean bothWordsIgnoreCase(String txt, String w1, String w2) { String ltxt = txt.toLowerCase(); return ltxt.contains(w1.toLowerCase()) && ltxt.contains(w2.toLowerCase()); } public static void main(String[] args) { String txt = "Java Provides Powerful Libraries for Developers"; String w1 = "java"; String w2 = "libraries"; boolean res = bothWordsIgnoreCase(txt, w1, w2); System.out.println(res); // Output: true } }
Output
true
3. Using String.indexOf()
The indexOf() method works similar to the contains() method, but it returns the position of the string if found; otherwise, it returns -1.
Steps to Implement
Step 1: Take the input text and the two words to search.
Step 2: Use the indexOf() method to search for each word.
Step 3: Return true if neither of them returns -1.
Step 4: Return false if any of them returns -1.
Implementation code
Below is the Java implementation using indexOf() ?
public class WordChecker { public static boolean bothWordsIndexOf(String txt, String w1, String w2) { return txt.indexOf(w1) != -1 && txt.indexOf(w2) != -1; } public static void main(String[] args) { String txt = "java provides powerful libraries for developers"; String w1 = "java"; String w2 = "libraries"; boolean res = bothWordsIndexOf(txt, w1, w2); System.out.println(res); // Output: true } }
Output
true
4.Use String.split() and containsAll()
In this method we split the string into an array of words and then uses the containsAll() method to check if both words are in the list. This approach is different from the previous approaches. This method helps us to match the words exactly and not parts of a word. Which means, in previous approaches, even if we input ?lib' to search for ?libraries,' it will return true as ?lib' is a part of ?libraries'. It is inefficient if you need to match the exact word only. This can be solved using the split() method that we will use in this approach.
Steps to Implement
Step 1: We take the input text as a string and the two words in an array.
Step 2: Divide the text into an array using split(" ") function.
Step 3: Convert the array into a list.
Step 4: Using containsAll(), check if both words are present in the text or not.
Implementation code
Below is the Java implementation using split() and containsAll() ?
import java.util.Arrays; import java.util.List; public class WordChecker { public static boolean wordsArray(String txt, String[] ws) { List<String> txtList = Arrays.asList(txt.split(" ")); return txtList.containsAll(Arrays.asList(ws)); } public static void main(String[] args) { String txt = "java provides powerful libraries for developers"; String w1 = "java"; String w2 = "libraries"; boolean res = wordsArray(txt, new String[]{w1, w2}); System.out.println(res); // Output: true } }
Output
true
5.Using Regular Expressions
We can also use regular expressions to check if two words exist in a text. Similar to the previous approach, this method also treats words as complete and not as parts. We use the ?\b' word boundary to ensure that only the whole words are matched.
Steps to implement
Step 1: Make a regular expression with \b for word boundaries.
Step 2: Compile it with Pattern.CASE_INSENSITIVE to ignore case differences.
Step 3: Use Matcher.find() to search for the words.
Implementation code
Below is the Java implementation using regular expressions ?
import java.util.regex.*; public class WordChecker { public static boolean bothWordsRegex(String txt, String w1, String w2) { String r1 = "\b" + Pattern.quote(w1) + "\b"; String r2 = "\b" + Pattern.quote(w2) + "\b"; Pattern p1 = Pattern.compile(r1, Pattern.CASE_INSENSITIVE); Pattern p2 = Pattern.compile(r2, Pattern.CASE_INSENSITIVE); Matcher m1 = p1.matcher(txt); Matcher m2 = p2.matcher(txt); return m1.find() && m2.find(); } public static void main(String[] args) { String txt = "java provides powerful libraries for developers"; String w1 = "java"; String w2 = "libraries"; boolean res = bothWordsRegex(txt, w1, w2); System.out.println(res); // Output: true } }
Output
false