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
How to create Slice using Make function in Golang?
Go program to find TXT records of a domain
Golang Web Server Example
Find odd and even numbers using goroutines and channels
Illustration of Producer Consumer Problem in Golang
Program in Go language to Calculate Standard Deviation using Math package
Most Helpful This Week
Passing multiple string arguments to a variadic functionRegular expression to extract filename from given path in GolangRegular expression to extract numbers from a string in GolangReplace first occurrence of string using RegexpPass different types of arguments in variadic functionHow to check lowercase characters in a string in Golang?How to get struct variable information using reflect package?Regular expression to extract DNS host-name or IP Address from stringHow to convert Colorful PNG image to Gray-scale?How to use function from another file golang?