course/test/spilt_string.go

34 lines
577 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 {
ret := make([]string, 0)
index := strings.Index(str, sep)
for index >= 0 {
ret = append(ret, str[:index])
str = str[index+1:]
index = strings.Index(str, sep)
}
ret = append(ret, str)
return ret
}