Remove a given word from a String

Given a String and a Word, the task is remove that Word from the String.

Examples:

Input: String = "Geeks For Geeks", Word = "For"
Output: "Geeks Geeks"

Input: String = "A computer Science Portal", Word = "Geeks"
Output: "A computer Science Portal"

Approach : In Java, this can be done using String replaceAll method by replacing given word with a blank space.

Below is the solution to the above problem:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to remove
// a given word from a string
public class GFG {
    public static String removeWord(String string, String word)
    {
  
        // Check if the word is present in string
        // If found, remove it using removeAll()
        if (string.contains(word)) {
  
            // To cover the case
            // if the word is at the
            // beginning of the string
            // or anywhere in the middle
            String tempWord = word + " ";
            string = string.replaceAll(tempWord, "");
  
            // To cover the edge case
            // if the word is at the
            // end of the string
            tempWord = " " + word;
            string = string.replaceAll(tempWord, "");
        }
  
        // Return the resultant string
        return string;
    }
  
    public static void main(String args[])
    {
  
        // Test Case 1:
        // If the word is in the middle
        String string1 = "Geeks for Geeks.";
        String word1 = "for";
  
        // Test Case 2:
        // If the word is at the beginning
        String string2 = "for Geeks Geeks.";
        String word2 = "for";
  
        // Test Case 3:
        // If the word is at the end
        String string3 = "Geeks Geeks for.";
        String word3 = "for";
  
        // Test Case 4:
        // If the word is not present
        String string4 = "A computer Science Portal.";
        String word4 = "Geeks";
  
        // Test case 1
        System.out.println("String: " + string1
                           + "\nWord: " + word1
                           + "\nResult String: "
                           + removeWord(string1, word1));
  
        // Test case 2
        System.out.println("\nString: " + string2
                           + "\nWord: " + word2
                           + "\nResult String: "
                           + removeWord(string2, word2));
  
        // Test case 3
        System.out.println("\nString: " + string3
                           + "\nWord: " + word3
                           + "\nResult String: "
                           + removeWord(string3, word3));
  
        // Test case 4
        System.out.println("\nString: " + string4
                           + "\nWord: " + word4
                           + "\nResult String: "
                           + removeWord(string4, word4));
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to remove
// a given word from a string
using System;
  
class GFG 
{
    public static String removeWord(String str, String word)
    {
  
        // Check if the word is present in string
        // If found, remove it using removeAll()
        if (str.Contains(word))
        {
  
            // To cover the case
            // if the word is at the
            // beginning of the string
            // or anywhere in the middle
            String tempWord = word + " ";
            str = str.Replace(tempWord, "");
  
            // To cover the edge case
            // if the word is at the
            // end of the string
            tempWord = " " + word;
            str = str.Replace(tempWord, "");
        }
  
        // Return the resultant string
        return str;
    }
  
    // Driver code
    public static void Main(String []args)
    {
  
        // Test Case 1:
        // If the word is in the middle
        String string1 = "Geeks for Geeks.";
        String word1 = "for";
  
        // Test Case 2:
        // If the word is at the beginning
        String string2 = "for Geeks Geeks.";
        String word2 = "for";
  
        // Test Case 3:
        // If the word is at the end
        String string3 = "Geeks Geeks for.";
        String word3 = "for";
  
        // Test Case 4:
        // If the word is not present
        String string4 = "A computer Science Portal.";
        String word4 = "Geeks";
  
        // Test case 1
        Console.WriteLine("String: " + string1
                        + "\nWord: " + word1
                        + "\nResult String: "
                        + removeWord(string1, word1));
  
        // Test case 2
        Console.WriteLine("\nString: " + string2
                        + "\nWord: " + word2
                        + "\nResult String: "
                        + removeWord(string2, word2));
  
        // Test case 3
        Console.WriteLine("\nString: " + string3
                        + "\nWord: " + word3
                        + "\nResult String: "
                        + removeWord(string3, word3));
  
        // Test case 4
        Console.WriteLine("\nString: " + string4
                        + "\nWord: " + word4
                        + "\nResult String: "
                        + removeWord(string4, word4));
    }
}
  
// This code contributed by Rajput-Ji

chevron_right


Output:

String: Geeks for Geeks.
Word: for
Result String: Geeks Geeks.

String: for Geeks Geeks.
Word: for
Result String: Geeks Geeks.

String: Geeks Geeks for.
Word: for
Result String: Geeks Geeks.

String: A computer Science Portal.
Word: Geeks
Result String: A computer Science Portal.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji