Join Strings with Delimiter
To join two or more strings with a delimiter in Go language, use strings.Join() function. Call Join() function and pass the strings as a slice for first argument, and the delimiter string as second argument.
In this tutorial, we will learn how to join two or more strings with a delimiter, with examples.
The syntax to use Join() function to join strings is
strings.Join(strs []string, delimiter string)
where strs
is the string slice which contains the strings to join, and delimiter
is the delimiter string with which the strings in strs
will be joined.
Examples
Join Two Strings
In the following example, we remove take two strings in an array strs
, and join them with a delimiter strings delimiter
.
Example.go
package main
import "strings"
func main() {
strs := []string{"apple", "banana"}
delimiter := "-"
result := strings.Join(strs, delimiter)
println(result)
}
Output
apple-banana
Join more than Two Strings
In the following example, we take four strings, and a lengthy delimiter to join the strings.
Example.go
package main
import "strings"
func main() {
strs := []string{"apple", "banana", "cherry", "mango"}
delimiter := "+++"
result := strings.Join(strs, delimiter)
println(result)
}
Output
apple+++banana+++cherry+++mango
Conclusion
In this Go Tutorial, we learned how to join strings with a delimiter in Go language using strings.Join() function, with example programs.