Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Program to Remove a Given Word From a String

Java Program to Remove a Given Word From a String

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:

  1. Naive approach using searching techniques
  2. 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);
    }
}


Output

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 

  1. This method takes two input old_word and the new word in the regex format.
  2. Replace all the old_word with the new word.
  3. 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);
    }
}


Output

This is  Geeks For Geeks

Time Complexity: O(n)

Auxiliary Space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments