w3resource

Java: Remove a word from a given text

Java String: Exercise-111 with Solution

Write a Java program that removes a specified word from given text. Return the updated string.

Visual Presentation:

Java String Exercises: Remove a  word from a given text

Sample Data:

("Exercises Practice Solution", " Solution") -> "Exercises Practice"
("Red Green Blue", "Green") -> "Red Blue"
("Java Number Exercises”, “Java”) -> "Number Exercises"

Sample Solution-1:

Java Code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner myObj = new Scanner(System.in); // Create a Scanner object for user input
        
        // Prompt the user to input a string and a word to remove
        System.out.println("Input a string: ");
        String text = myObj.nextLine(); // Read the input string
        System.out.println("Input a word to remove: ");
        String word = myObj.nextLine(); // Read the word to remove
        
        // Display the new string after removing the specified word
        System.out.println("New string, after removing the said word:\n" + test(text, word));
    }

    // Method to remove a specified word from a given string
    public static String test(String text, String word) {
        String result_str = text.replace(word, ""); // Replace occurrences of the word with an empty string
        result_str = result_str.replaceAll("\\s+", " "); // Remove extra spaces in the resultant string
        return result_str; // Return the modified string
    }
}

Sample Output:

Input a string: 
 Exercises Practice Solution
Input a word to remove: 
 Solution
New string, after removing the said word:
Exercises Practice 

Flowchart:

Flowchart: Java String Exercises - Remove a  word from a given text

Sample Solution-2:

Java Code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner myObj = new Scanner(System.in); // Create a Scanner object for user input
        
        // Prompt the user to input a string and a word to remove
        System.out.println("Input a string: ");
        String text = myObj.nextLine(); // Read the input string
        System.out.println("Input a word to remove: ");
        String word = myObj.nextLine(); // Read the word to remove
        
        // Display the new string after removing the specified word
        System.out.println("New string, after removing the said word:\n" + test(text, word));
    }

    // Method to remove a specified word from a given string
    public static String test(String text, String word) {
        String result_str = text.replace(word, ""); // Replace occurrences of the word with an empty string
        result_str = result_str.replaceAll("\\s+", " "); // Remove extra spaces in the resultant string
        return result_str; // Return the modified string
    }
}

Sample Output:

Input a string: 
 Red Green Blue
Input a word to remove: 
 Green
New string, after removing the said word:
Red Blue

Flowchart:

Flowchart: Java String Exercises - Remove a  word from a given text

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous Java Exercise: Count duplicate characters in a String.
Next Java Exercise: Check first string contains letters from the second.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.