Created
December 5, 2017 19:31
-
-
Save denniswebb/14e7da27ab0a78099731b45e41ec0120 to your computer and use it in GitHub Desktop.
Golang function to split a string by size on a separator.
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
func StringSplitAfterNSize(s, sep string, size int) (resp []string) { | |
rl := "" | |
for _, line := range strings.SplitAfter(s, sep) { | |
if len(rl+line) > size { | |
resp = append(resp, rl) | |
rl = "" | |
} | |
rl += line | |
} | |
if len(resp) > 0 && len(rl) == 0 { | |
return | |
} | |
resp = append(resp, rl) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment