strings.Split
时间: 2024-03-11 09:15:23 浏览: 28
strings.Split is a built-in function in Go that is used to split a string into substrings based on a specified delimiter. It returns a slice of strings where each element is a substring separated by the delimiter. Here's an example:
```go
package main
import (
"fmt"
"strings"
)
func main() {
str := "apple,banana,grape,orange"
elements := strings.Split(str, ",")
fmt.Println(elements) // Output: ["apple" "banana" "grape" "orange"]
}
```
In the above example, the `strings.Split` function is used to split the `str` variable into substrings using the comma (",") as the delimiter. The resulting substrings are stored in the `elements` slice, which is then printed to the console. Each element in the slice represents a separated substring.