36 lines
619 B
Go
36 lines
619 B
Go
package Splitstring
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
//切割字符串
|
|
//example
|
|
// abc,b =>[a,c]
|
|
|
|
//func Split(str string, sep string) []string {
|
|
// tmp := make([]string, 0)
|
|
// for _, v := range str {
|
|
// if string(v) == sep {
|
|
// continue
|
|
// } else {
|
|
// tmp = append(tmp, string(v))
|
|
// }
|
|
// }
|
|
// return tmp
|
|
//}
|
|
|
|
// abc,b =>[a,c]
|
|
|
|
func Split(str string, sep string) []string {
|
|
var ret = make([]string, 0, strings.Count(str, sep)+1)
|
|
index := strings.Index(str, sep)
|
|
for index >= 0 {
|
|
ret = append(ret, str[:index])
|
|
str = str[index+len(sep):]
|
|
index = strings.Index(str, sep)
|
|
}
|
|
ret = append(ret, str)
|
|
return ret
|
|
}
|