Replace all occurrences of a string with space

Given a string and a substring, the task is to replace all occurrences of the substring with space. We also need to remove trailing and leading spaces created due to this.

Examples:

Input: str = “LIELIEILIEAMLIECOOL”, sub = “LIE”
Output: I AM COOL
By replacing all occurrences of Sub in Str with empty spaces, we extract the secret message as I AM COOL.

Input: str = “XYZAXYZBXYZC”, sub = “XYZ”
Output: ABC
By replacing all occurrences of Sub in Str with empty spaces, we extract the secret message as ABC.

Approach:

  • In the given string Str, replace all occurrences of Sub with empty spaces.
  • Remove unwanted empty spaces in start and end of the string.
  • Print the modified string.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to extract the secret message
import java.io.*;
import java.util.*;
  
class GFG {
  
    // Function to extract the secret message
    static String extractSecretMessage(String Str, String Sub)
    {
        // Replacing all occurrences of
        // Sub in Str by empty spaces
        Str = Str.replaceAll(Sub, " ");
  
        // Removing unwanted spaces in the
        // start and end of the string
        Str = Str.trim();
  
        return Str;
    }
  
    // Driver code
    public static void main(String args[])
    {
        String Str = "LIELIEILIEAMLIECOOL";
        String Sub = "LIE";
        System.out.println(extractSecretMessage(Str, Sub));
    }
}

chevron_right


Python3

# Python3 implementation to extract
# the secret message

# Function to extract the secret message
def extractSecretMessage(Str, Sub):

# Replacing all occurrences of
# Sub in Str by empty spaces
Str= Str.replace(Sub, ” “)

# Removing unwanted spaces in the
# start and end of the string

return Str.strip()

# Driver code
Str = “LIELIEILIEAMLIECOOL”
Sub = “LIE”
print(extractSecretMessage(Str, Sub))

# This code is contributed
# by ihritik

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to extract the 
// secret message 
using System;
  
class GFG 
  
// Function to extract the secret message 
static string extractSecretMessage(string Str, 
                                   string Sub) 
    // Replacing all occurrences of 
    // Sub in Str by empty spaces 
    Str = Str.Replace(Sub, " "); 
  
    // Removing unwanted spaces in the 
    // start and end of the string 
    Str = Str.Trim(); 
  
    return Str; 
  
// Driver code 
public static void Main() 
    string Str = "LIELIEILIEAMLIECOOL"
    string Sub = "LIE"
    Console.WriteLine(extractSecretMessage(Str, Sub)); 
  
// This code is contributed by Ryuga

chevron_right


PHP

Output:

I AM COOL


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 : AnkitRai01, ihritik