How to remove multiple spaces in a string in GoLang?
Removing multiple spaces from a string excludes excessive white space so that there is only a single space between each word in the string.
Remove Multiple Spaces
Example
// Golang program to remove
// multiple white-spaces from string
package main
import (
"fmt"
"strings"
)
func standardizeSpaces(s string) string {
return strings.Join(strings.Fields(s), " ")
}
func main() {
str1 := " Hello, World ! "
fmt.Println(standardizeSpaces(str1))
str2 := "Hello,\tWorld ! "
fmt.Println(standardizeSpaces(str2))
str3 := " \t\n\t Hello,\tWorld\n!\n\t"
fmt.Println(standardizeSpaces(str3))
}
Output
Hello, World !
Hello, World !
Hello, World !
Most Helpful This Week
Invalid operation: <variable> (type <type>) does not support indexing error in Golang
Golang panic recover example
How do you send an HTTP GET request with an HTTP client in Go?
Golang program for implementation of Rabin-Karp
How to remove all line breaks from a string in Golang?
Invalid memory address or nil pointer dereference error in Golang
Most Helpful This Week
How to check if a map contains a key in Go?What is Rune? How to get ASCII value of any character in Go?Golang Slice interface and array concatenationHow to fetch an Integer variable as String in Go?How to get the current date and time with timestamp in local and other timezones ?How to handle HTTP Get response?How do you write multi-line strings in Go?How To Make an HTTP Server in Golang?How to create Map using the make function in Go?Regular Expression to get a string between parentheses in Golang