Skip to content

Instantly share code, notes, and snippets.

@adrianocr
Last active August 10, 2020 14:36
Show Gist options
  • Save adrianocr/c59a878630d923d50ac8107ba3a1467b to your computer and use it in GitHub Desktop.
Save adrianocr/c59a878630d923d50ac8107ba3a1467b to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"unicode"
)
func removeDoubleSpaces(x []byte) []byte {
var spaces int
for i, v := range x {
if unicode.IsSpace(rune(v)) {
stop := i
for _, vv := range x[i+1:] {
if !unicode.IsSpace(rune(vv)) {
break
}
stop++
}
spaces += len(x[i:stop])
copy(x[i:], x[stop:])
}
}
return x[:len(x)-spaces]
}
func main() {
str := []byte("this is a test with a few double and triple spaces inserted throughout. and 4 spaces: <- there")
fmt.Printf("%s", removeDoubleSpaces(str))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment