Skip to content

Instantly share code, notes, and snippets.

@rohanthewiz
Created March 16, 2017 17:30
Show Gist options
  • Save rohanthewiz/d9401801bbd6bc71c37a54538c69e362 to your computer and use it in GitHub Desktop.
Save rohanthewiz/d9401801bbd6bc71c37a54538c69e362 to your computer and use it in GitHub Desktop.
Split and trim a string containing any combination of tabs and spaces into trimmed words
ackage main
import (
"fmt"
"strings"
)
func main() {
str := "When not ok\t, val\tis 0"
fmt.Printf("%q\n", SplitAndTrim(str))
}
// Split string containing any combination of tabs and spaces into trimmed words
func SplitAndTrim(instr string) []string {
out := []string{}
for _, tab_chunk := range strings.Split(instr, "\t") {
for _, word := range strings.Split(tab_chunk, " ") {
out = append(out, strings.TrimSpace(word))
}
}
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment