How to split a string by multiple delimiters

前端 未结 5 1115
长发绾君心
长发绾君心 2021-02-20 03:13

I want to parse a string xxxxx:yyyyy:zzz.aaa.bbb.cc:dd:ee:ff to a struct in Go, how can I do it with multiple delimiter \':\' and \'.\'.

Edit:

I want to split the

5条回答
  •  借酒劲吻你
    2021-02-20 03:52

    Here is a generic function that will take a string as a set of runes to split on.

    func Splitter(s string, splits string) []string {
        m := make(map[rune]int)
        for _, r := range splits {
            m[r] = 1
        }
    
        splitter := func(r rune) bool {
            return m[r] == 1
        }
    
        return strings.FieldsFunc(s, splitter)
    }
    
    func TestSplit() {
        words := Splitter("orange apple-banana", " -")
    }
    

提交回复
热议问题