57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
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])
|
||
})
|
||
}
|