Java Program to Remove a Given Word From a String Last Updated : 28 Jul, 2022 Comments Improve Suggest changes Like Article Like Report 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 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); } } OutputThis 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); } } OutputThis is Geeks For Geeks Time Complexity: O(n) Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Java Program to Remove a Given Word From a String zack_aayush Follow Improve Article Tags : Java Java Programs Java-String-Programs Practice Tags : Java Similar Reads Java Program To Reverse Words In A Given String Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "p 4 min read Program to remove consonants from a String Given a string , the task is to remove all the consonants from the string and then print the string. Examples: Input: str= "Welcome to geeksforgeeks" Output: eoe o eeoee Input: str= "What is your name?" Output: a i ou ae? Recommended PracticeRemove consonants from a stringTry It!Approach: Traverse a 6 min read How to Remove all White Spaces from a String in Java? In Java, to remove all white spaces from a String, there are several ways like using replaceAll() or manual iteration over characters. In this article, we will learn how to remove all white spaces from a string in Java.Example:We will use the replaceAll() method with the regex pattern "\\s" to remov 3 min read How to Remove Duplicates from a String in Java? Working with strings is a typical activity in Java programming, and sometimes we need to remove duplicate characters from a string. In this article we have given a string, the task is to remove duplicates from it. Example Input: s = "geeks for geeks"Output: str = "geks for" Remove Duplicates From a 2 min read How to Remove Duplicates from a String in Java Using Regex ? In Java programming, Regex (regular expressions) is a very important mechanism. It can be used to match patterns in strings. Regular expressions provide a short and simple syntax for describing the patterns of the text. In this article, we will be learning how to remove duplicates from a String in J 2 min read Java Program to Replace All Line Breaks from Strings Given a string, write a Java program to replace all line breaks from the given String. Examples Input - Geeks For Geeks Output - Geeks For Geeks Line Break: A line break ("\n") is a single character that defines the line change. In order to replace all line breaks from strings replace() function can 2 min read How to Remove All Punctuation from a String using Regex in Java? In this article, we will explain how to remove all punctuation from a given String by using Java with the help of Regex. Here regex means regular expression. The regex is a powerful way to explain the search pattern. One more thing is that regular expressions are mostly used for Searching a required 4 min read Java Program to Search a Particular Word in a String Using Regex In Java string manipulation, searching for specific words is a fundamental task. Regular expressions (regex) offer a powerful and flexible approach to achieve this search. It matches the patterns simply by comparing substrings. In this article, we will learn how to search for a particular word in a 3 min read Java Program to Separate the Individual Characters from a String The string is a sequence of characters including spaces. Objects of String are immutable in java, which means that once an object is created in a string, it's content cannot be changed. In this particular problem statement, we are given to separate each individual characters from the string provided 2 min read Java program to print Even length words in a String Given a string s, write a Java program to print all words with even length in the given string. Example to print even length words in a String Input: s = "i am Geeks for Geeks and a Geek" Output: am GeekExample:Java// Java program to print // even length words in a string class GfG { public static v 3 min read Like