字符串.Join() Golang 中的函数将字符串切片中存在的所有元素连接成单个字符串。该函数在字符串包中可用。
句法:
func Join(s []string, sep string) string
这里, s是我们可以连接元素的字符串, sep 是放置在最终字符串元素之间的分隔符。
返回值:它返回一个字符串。
示例 1:
// Golang program to illustrate the
// use of strings.Join Function
package main
// importing fmt and strings
import (
"fmt"
"strings"
)
// calling main method
func main() {
// array of strings.
str := []string{"Geeks", "For", "Geeks"}
// joining the string by separator
fmt.Println(strings.Join(str, "-"))
}
输出:
Geeks-For-Geeks
示例 2:
// Golang program to illustrate the
// use of strings.Join Function
package main
// importing fmt and strings
import (
"fmt"
"strings"
)
// calling main method
func main() {
// array of strings.
str := []string{"A", "Computer-science", "portal", "for", "Geeks"}
// joining the string by separator in middle.
fmt.Println(strings.Join(str, " "))
}
输出:
A Computer-science portal for Geeks