How to split Golang strings without deleting separator?

前端 未结 3 1942
南笙
南笙 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条回答
  •  日久生厌
    2021-01-15 09:34

    You are looking for SplitAfter.

    s := strings.SplitAfter("Potato:Salad:Popcorn:Cheese", ":")
      for _, element := range s {
      fmt.Println(element)
    }
    // Potato:
    // Salad:
    // Popcorn:
    // Cheese
    

    Go Playground

提交回复
热议问题