Skip to content

Instantly share code, notes, and snippets.

@jn0
Created December 17, 2019 12:06
Show Gist options
  • Save jn0/40f50c3da6cae572a6d4dd20db1041f3 to your computer and use it in GitHub Desktop.
Save jn0/40f50c3da6cae572a6d4dd20db1041f3 to your computer and use it in GitHub Desktop.
Handling spaces in strings
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