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
Pascal triangle in Go Programming Language
Concurrently printing array elements using goroutines and channels
How to convert String to Boolean Data Type Conversion in Go?
Golang program for implementation of Longest Common Sub-sequence
Defining a type that satisfies an interface in Go Programming Language
Sierpinski triangle in Go Programming Language
How to Improve the Performance of Your Golang Application?
Program in Go language to Program to Add Two Matrix Using Multi-dimensional Arrays
How do you handle HTTP responses in Go?
GO Program to Check Armstrong Number
Most Helpful This Week
Passing multiple string arguments to a variadic functionRegular expression to extract DNS host-name or IP Address from stringExample: How to use TeeReader from IO Package in Golang?Example: Stack and Caller from RUNTIME packageReplace numbers by zero from stringExample to handle GET and POST request in GolangExample of Pointers with StructRegex to extract image name from HTML in GolangNormal function parameter with variadic function parameterDifferent ways for Integer to String Conversions