Java Program to Remove a Given Word From a String

04 Jan, 2022
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 techniquesOptimal approach using String.replaceAll() Method 
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 classesimport java.io.*; // main classclass 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 
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.
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 classesimport java.io.*; // Main classclass 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
gulshankumarar231
varshagumber28
Java-String-Programs
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
Stream In Java
ArrayList in Java
Initializing a List in Java
Java Programming Examples
Convert a String to Character Array in Java
Convert Double to Integer in Java
Implementing a Linked List in Java using Class