diff --git a/docs/slice.md b/docs/slice.md index 709c77e..dbfb768 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -90,6 +90,7 @@ import ( - [UpdateAt](#UpdateAt) - [Without](#Without) - [KeyBy](#KeyBy) +- [Join](#Join)
@@ -2417,3 +2418,36 @@ func main() { // map[1:a 2:ab 3:abc] } ``` + +### Join + +

Join the slice item with specify separator.

+ +Signature: + +```go +func Join[T any](s []T, separator string) string +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + + result1 := slice.Join(nums, ",") + result2 := slice.Join(nums, "-") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 1,2,3,4,5 + // 1-2-3-4-5 +} +``` diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index f66138c..ee842a8 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -90,6 +90,7 @@ import ( - [UpdateAt](#UpdateAt) - [Without](#Without) - [KeyBy](#KeyBy) +- [Join](#Join)
@@ -2390,7 +2391,7 @@ func main() { ### KeyBy -

将切片每个元素调用函数后转为map

+

将切片每个元素调用函数后转为map。

函数签名: @@ -2417,3 +2418,36 @@ func main() { // map[1:a 2:ab 3:abc] } ``` + +### Join + +

用指定的分隔符链接切片元素。

+ +函数签名: + +```go +func Join[T any](s []T, separator string) string +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + + result1 := slice.Join(nums, ",") + result2 := slice.Join(nums, "-") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 1,2,3,4,5 + // 1-2-3-4-5 +} +``` diff --git a/slice/slice.go b/slice/slice.go index 68cd366..565df09 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -9,6 +9,7 @@ import ( "math/rand" "reflect" "sort" + "strings" "time" "golang.org/x/exp/constraints" @@ -1188,3 +1189,13 @@ func KeyBy[T any, U comparable](slice []T, iteratee func(item T) U) map[U]T { return result } + +// Join the slice item with specify separator. +// Play: todo +func Join[T any](s []T, separator string) string { + str := Map(s, func(_ int, item T) string { + return fmt.Sprint(item) + }) + + return strings.Join(str, separator) +} diff --git a/slice/slice_example_test.go b/slice/slice_example_test.go index 9d94ebe..c2a3e60 100644 --- a/slice/slice_example_test.go +++ b/slice/slice_example_test.go @@ -1032,3 +1032,17 @@ func ExampleKeyBy() { // Output: // map[1:a 2:ab 3:abc] } + +func ExampleJoin() { + nums := []int{1, 2, 3, 4, 5} + + result1 := Join(nums, ",") + result2 := Join(nums, "-") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 1,2,3,4,5 + // 1-2-3-4-5 +} diff --git a/slice/slice_test.go b/slice/slice_test.go index 509c0d3..fee8c23 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -1161,3 +1161,17 @@ func TestRepeat(t *testing.T) { assert.Equal([]string{}, Repeat("a", 0)) assert.Equal([]string{"a", "a", "a", "a", "a", "a"}, Repeat("a", 6)) } + +func TestJoin(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestJoin") + + nums := []int{1, 2, 3, 4, 5} + + result1 := Join(nums, ",") + result2 := Join(nums, "-") + + assert.Equal("1,2,3,4,5", result1) + assert.Equal("1-2-3-4-5", result2) +}