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