I was wondering how to remove:
Avoiding to use time wasting regexp or external library
I've choose to use plain golang instead of regexp, cause there are special character that are not ASCII in every language.
Go Golang!
func RemoveDoubleWhiteSpace(str string) string {
var b strings.Builder
b.Grow(len(str))
for i := range str {
if !(str[i] == 32 && (i+1 < len(str) && str[i+1] == 32)) {
b.WriteRune(rune(str[i]))
}
}
return b.String()
}
And the related test
func TestRemoveDoubleWhiteSpace(t *testing.T) {
data := []string{` test`, `test `, `te st`}
for _, item := range data {
str := RemoveDoubleWhiteSpace(item)
t.Log("Data ->|"+item+"|Found: |"+str+"| Len: ", len(str))
if len(str) != 5 {
t.Fail()
}
}
}