In Golang, you can split a string into substrings based on a delimiter using the strings.Split()
function in the built-in strings
package.
Here's an example of how to split a string into substrings based on a delimiter:
package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello-World-Go"
delimiter := "-"
// Split the string into substrings based on the delimiter
substrings := strings.Split(str, delimiter)
// Print the substrings
for _, substring := range substrings {
fmt.Println(substring)
}
}
In this example, we have a string str
with the value "Hello-World-Go"
. We want to split this string into substrings based on the delimiter -
.
By using the strings.Split()
function and passing the str
and delimiter
as arguments, we can split the string into substrings. The function will return a slice of strings containing the substrings.
In the example, we loop over the substrings
slice using a for
loop and print each substring using the fmt.Println()
function.
The output of this code will be:
Hello
World
Go
Each substring has been split based on the delimiter -
.