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
// 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
filter_none
C#
// 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
filter_none
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.
Recommended Posts:
- Length Of Last Word in a String
- Print last character of each word in a string
- Capitalize the first and last character of each word in a string
- Extracting each word from a String using Regex in Java
- Print first letter of each word in a string using regex
- Get the first letter of each word in a string using regex in Java
- Program to remove consonants from a String
- Remove all non-alphabetical characters of a String in Java
- Remove Leading Zeros From String in Java
- How to remove all white spaces from a String in Java?
- Remove extra delimiter from a String in Java
- Trim (Remove leading and trailing spaces) a string in Java
- Java program to count the characters in each word in a given sentence
- ConcurrentLinkedQueue remove() method in Java
- How to Remove Duplicates from ArrayList in Java
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



