How to split Golang strings without deleting separator?

前端 未结 3 1939
南笙
南笙 2021-01-15 08:55

According to the answer at How to split a string and assign it to variables in Golang? splitting a string results in an array of strings where the separator is not present i

3条回答
  •  Happy的楠姐
    2021-01-15 09:27

    Try this to get the proper result.

    package main
    
        import (
            "fmt"
            "strings"
        )
    
        func main() {
            str := "Potato:Salad:Popcorn:Cheese"
            a := strings.SplitAfter(str, ":")
            for i := 0; i < len(a); i++ {
                fmt.Println(a[i])
            }
        }
    

提交回复
热议问题