Created
March 16, 2017 17:30
-
-
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
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
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