Given a string and a word that task to remove the word from the string if it exists otherwise return -1 as an output. For more clarity go through the illustration given below as follows.
Illustration:
Input : This is the Geeks For Geeks word="the" Output : This is Geeks For Geeks Input : Hello world Hello" word = Hello Output : world Explanation: The given string contains the word two times Input : Hello world word = GFG Output : Hello world Explanation: word doesn't exists in the string
Now in order to reach the goal, we will be discussing ou two approaches namely as follows:
- Naive approach using searching techniques
- Optimal approach using String.replaceAll() MethodÂ
Method 1: Using searching techniquesÂ
- Convert the string into a string array.
- Iterate the array and check the word not equal to the given word.
- Concatenate the word into a new string array name as a new string.
- Print the new string.
Example
Java
// Java Program to Remove a Given Word From a String // using searching techniques Â
// Importing input output classes import java.io.*; Â
// main class class GFG { Â
    // Method 1     // To remove the word     static void remove(String str, String word)     {         // Split the string using split() method         String msg[] = str.split( " " );         String new_str = "" ; Â
        // Iterating the string using for each loop         for (String words : msg) { Â
            // If desired word is found             if (!words.equals(word)) { Â
                // Concat the word not equal to the given                 // word                 new_str += words + " " ;             }         } Â
        // Print the new String         System.out.print(new_str);     } Â
    // Method 2     // Main driver method     public static void main(String[] args)     {         // Custom string as input         String str = "This is the Geeks For Geeks" ; Â
        // Word to be removed from above string         String word = "the" ; Â
        // Calling the method 1 by passing both strings to         // it         remove(str, word);     } } |
This is Geeks For Geeks
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n), where n is the length of the given string
Method 2: Using String.replaceAll() MethodÂ
- This method takes two input old_word and the new word in the regex format.
- Replace all the old_word with the new word.
- Returns the resultant string.
ExampleÂ
Java
// Java Program to Remove a Given Word From a String Â
// Importing input output classes import java.io.*; Â
// Main class class GFG { Â
    // Main driver method     public static void main(String[] args)     {         // Given String as input from which         // word has to be removed         String str = "This is the Geeks For Geeks" ; Â
        // Desired word to be removed         String word = "the" ; Â
        // Replace all words by "" string         // using replaceAll() method         str = str.replaceAll( "the" , "" ); Â
        // Trim the string using trim() method         str = str.trim(); Â
        // Printing the final string         System.out.print(str);     } } |
This is Geeks For Geeks
Time Complexity: O(n)
Auxiliary Space: O(n)