To remove a specific substring from a string in Go, you can use the `strings.Replace` function.
In this example,
strings
package to access string manipulation functions.str
with a value of "Hello World"
.Replace
function from the strings
package to replace the substring "World"
with an empty string ""
.Replace
function returns a new string with the specified replacements.str
to the console.package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello World"
str = strings.Replace(str, "World", "", -1)
fmt.Println("Modified string:", str)
}
Modified string: Hello
In this example,
strings
package to access string manipulation functions.text
with a value of "I like apple pie"
.Replace
function from the strings
package to replace the substring "apple"
with an empty string ""
.Replace
function creates and returns a new string with the specified replacements.text
to the console.package main
import (
"fmt"
"strings"
)
func main() {
text := "I like apple pie"
text = strings.Replace(text, "apple", "", -1)
fmt.Println("Modified string:", text)
}
Modified string: I like pie
In this tutorial, we learned How to Remove specific Substring in Go language with well detailed examples.