I was wondering how to remove:
You can get quite far just using the strings
package as strings.Fields
does most of the work for you:
package main
import (
"fmt"
"strings"
)
func standardizeSpaces(s string) string {
return strings.Join(strings.Fields(s), " ")
}
func main() {
tests := []string{" Hello, World ! ", "Hello,\tWorld ! ", " \t\n\t Hello,\tWorld\n!\n\t"}
for _, test := range tests {
fmt.Println(standardizeSpaces(test))
}
}
// "Hello, World !"
// "Hello, World !"
// "Hello, World !"