Last active
August 10, 2020 14:36
-
-
Save adrianocr/c59a878630d923d50ac8107ba3a1467b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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