切割切片組裝字串
使用自定義字元切割切片組裝成字串
package main
import (
"fmt"
"strings"
)
func main() {
str := []string{"z","a","d"}
fmt.Println(sliceToString(str,","))
// 輸出結果 z,a,d
}
func sliceToString(data []string,symbol string) (str string) {
resultString := ""
for _,v := range data {
resultString += v + symbol
}
// Trim 刪除字串
return strings.Trim(resultString,",")
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結