In Golang, the strings.Split
function is a powerful tool for splitting a string based on a specified separator. Part of the strings
package, it breaks a string into a slice of substrings, which makes parsing text easy. The syntax is:
package main
import (
"fmt"
"strings"
)
func main() {
s := "Golang,Strings,Split"
result := strings.Split(s, ",")
fmt.Println("Result: ", result)
}
It returns [Golang Strings Split]. The separator
parameter can be any string, allowing for flexible splitting.
If separator is double quotes use a back quote to wrap second argument:
strings.Split(s,`"`)
If the separator is not found, Split
returns a slice with the original string. Split
is useful in processing CSVs, logs, or structured text where strings need to be broken down.
Leave a Comment