light-core/pkg/mySql/StringSplit.go
2024-12-17 22:47:16 +08:00

57 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package mySql
import (
"strings"
)
type StringSplit struct {
str string
sep string
}
func NewStringSplit(str string, sep string) *StringSplit {
return &StringSplit{str: str, sep: sep}
}
func (t *StringSplit) chooseIndexStr(index int) (string, bool) {
//劈开
countSplit := strings.Split(t.str, t.sep)
//获取对应的字符串index是从0开始算。
if len(countSplit) <= index {
return "", false
}
return countSplit[index], true
}
// RunIndexFunc 取第index个从0开始数执行。
func (t *StringSplit) RunIndexFunc(index int, f func(str string)) {
str, exist := t.chooseIndexStr(index)
if exist {
f(str)
}
}
// RunCountFunc 如果分割出来是count个则执行
func (t *StringSplit) RunCountFunc(count int, f func(strArr []string)) {
countSplit := strings.Split(t.str, t.sep)
if len(countSplit) == count {
f(countSplit)
}
}
// RunCount1Func 常用方法封装快捷方法count=1
func (t *StringSplit) RunCount1Func(f func(str string)) {
t.RunCountFunc(1, func(strArr []string) {
f(strArr[0])
})
}
// RunCount2Func 常用方法封装快捷方法count=2
func (t *StringSplit) RunCount2Func(f func(str1, str2 string)) {
t.RunCountFunc(2, func(strArr []string) {
f(strArr[0], strArr[1])
})
}