Replace all occurrences of a string with space

28 May, 2022
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: 
C++
Java
Python3
C#
PHP
// C++ implementation to extract// the secret message#include <bits/stdc++.h>using namespace std; // Trim method implementation to remove// trailing and leading white-spacesstring trim(const string &s){    auto start = s.begin();    while (start != s.end() &&           isspace(*start))        start++;     auto end = s.end();    do    {        end--;    } while (distance(start, end) > 0 &&                    isspace(*end));     return string(start, end + 1);} // Function to extract the secret messagestring extractSecretMessage(string str,                            string sub){    // Replacing all occurrences of    // Sub in Str by empty spaces    size_t pos;    while ((pos = str.find(sub)) != string::npos)        str.replace(pos, 3, " ");     // Removing unwanted spaces in the    // start and end of the string    str = trim(str);     return str;} // Driver codeint main(int argc, char const *argv[]){    string str = "LIELIEILIEAMLIECOOL";    string sub = "LIE";    cout << extractSecretMessage(str, sub)         << endl;    return 0;} // This code is contributed by// sanjeev2552
// Java implementation to extract the secret messageimport 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));    }}
# Python3 implementation to extract# the secret message # Function to extract the secret messagedef 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 codeStr = "LIELIEILIEAMLIECOOL"Sub = "LIE"print(extractSecretMessage(Str, Sub)) # This code is contributed# by ihritik
// C# implementation to extract the// secret messageusing System; class GFG{ // Function to extract the secret messagestatic 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 codepublic static void Main(){    string Str = "LIELIEILIEAMLIECOOL";    string Sub = "LIE";    Console.WriteLine(extractSecretMessage(Str, Sub));}} // This code is contributed by Ryuga
<?php// PHP implementation to extract the// secret message // Function to extract the secret messagefunction extractSecretMessage($Str, $Sub){    // Replacing all occurrences of    // Sub in Str by empty spaces    $Str = str_replace($Sub, " ", $Str);     // Removing unwanted spaces in the    // start and end of the string         return trim($Str);} // Driver code$Str = "LIELIEILIEAMLIECOOL";$Sub = "LIE";echo extractSecretMessage($Str, $Sub); // This code is contributed// by ihritik?>
I AM COOL
Time Complexity: O(N), as we are using  replace function which will cost us O(N) time.Auxiliary Space: O(1), as we are not using any extra space.
ankthon
ihritik
sanjeev2552
rohitsingh07052
Java-Strings
Java Programs
Strings
Java-Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Iterate Over the Characters of a String in Java
How to Convert Char to String in Java?
How to Get Elements By Index from HashSet in Java?
Java Program to Write into a File
How to Write Data into Excel Sheet using Java?
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Different Methods to Reverse a String in C++