To replace multiple spaces with a single space in Go, you can use regular expressions and the `regexp` package.
In this example,
regexp
package.\s+
to match multiple spaces.ReplaceAllString
function to replace multiple spaces with a single space in the string str
.str
.package main
import (
"fmt"
"regexp"
)
func main() {
str := "This is a test string"
re := regexp.MustCompile(`\s+`)
modifiedStr := re.ReplaceAllString(str, " ")
fmt.Println(modifiedStr)
}
This is a test string
In this tutorial, we learned How to Replace multiple Spaces with a Single Space in String in Go language with well detailed examples.