Created
December 17, 2019 12:06
-
-
Save jn0/40f50c3da6cae572a6d4dd20db1041f3 to your computer and use it in GitHub Desktop.
Handling spaces in strings
This file contains 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 spaces | |
import "regexp" | |
import "strings" | |
var spaces_re = regexp.MustCompile(`\s+`) // *any* space | |
const sp = " " // *the* space | |
// Replace *any* sequence of white-space chars with single vanilla space ('\x20') | |
func Strip(s string) string { return strings.TrimSpace(spaces_re.ReplaceAllString(s, sp)) } | |
// like Python's "string".split() | |
func SplitSpace(s string) []string { return strings.Split(Strip(s), sp) } | |
/* EOF */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment