1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: strutil.SplitEx. Split a given string whether the result contains empty string (#31)

Co-authored-by: Frank.Town <tangzhi21@sfmail.sf-express.com>
This commit is contained in:
franktz
2022-05-11 15:19:53 +08:00
committed by GitHub
parent 5eb056277d
commit 84a69d1fa5
2 changed files with 61 additions and 0 deletions

View File

@@ -247,3 +247,52 @@ func Unwrap(str string, wrapToken string) string {
return str
}
// SplitEx split a given string whether the result contains empty string
func SplitEx(s, sep string, removeEmptyString bool) []string {
if sep == "" {
return []string{}
}
n := strings.Count(s, sep) + 1
a := make([]string, n)
n--
i := 0
sepSave := 0
ignore := false
for i < n {
m := strings.Index(s, sep)
if m < 0 {
break
}
ignore = false
if removeEmptyString {
if s[:m+sepSave] == "" {
ignore = true
}
}
if !ignore {
a[i] = s[:m+sepSave]
s = s[m+len(sep):]
i++
} else {
s = s[m+len(sep):]
}
}
var ret []string
if removeEmptyString {
if s != "" {
a[i] = s
ret = a[:i+1]
} else {
ret = a[:i]
}
} else {
a[i] = s
ret = a[:i+1]
}
return ret
}