1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 21:02:27 +08:00

doc: add docment for Replace and ReplaceAll

This commit is contained in:
dudaodong
2022-10-17 10:40:35 +08:00
parent eced25b76d
commit 6248293c49
2 changed files with 113 additions and 0 deletions

View File

@@ -53,6 +53,8 @@ import (
- [Map](#Map)
- [Reverse](#Reverse)
- [Reduce](#Reduce)
- [Replace](#Replace)
- [ReplaceAll](#ReplaceAll)
- [Shuffle](#Shuffle)
- [SortByField](#SortByField)
- [Some](#Some)
@@ -966,6 +968,60 @@ func main() {
### <span id="Replace">Replace</span>
<p>Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.</p>
<b>Signature:</b>
```go
func Replace[T comparable](slice []T, old T, new T, n int) []T
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b", "a", "c", "d", "a"}
fmt.Println(slice.Replace(strs, "a", "x", 0)) //{"a", "b", "a", "c", "d", "a"}
fmt.Println(slice.Replace(strs, "a", "x", 1)) //{"x", "b", "a", "c", "d", "a"}
fmt.Println(slice.Replace(strs, "a", "x", -1)) //{"x", "b", "x", "c", "d", "x"}
}
```
### <span id="ReplaceAll">ReplaceAll</span>
<p>Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.</p>
<b>Signature:</b>
```go
func ReplaceAll[T comparable](slice []T, old T, new T) []T
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b", "a", "c", "d", "a"}
fmt.Println(slice.ReplaceAll(strs, "a", "x")) //{"x", "b", "x", "c", "d", "x"}
fmt.Println(slice.Replace(strs, "e", "x")) //{"a", "b", "a", "c", "d", "a"}
}
```
### <span id="Shuffle">Shuffle</span>
<p>Creates an slice of shuffled values.</p>

View File

@@ -53,6 +53,8 @@ import (
- [Map](#Map)
- [Reverse](#Reverse)
- [Reduce](#Reduce)
- [Replace](#Replace)
- [ReplaceAll](#ReplaceAll)
- [Shuffle](#Shuffle)
- [SortByField](#SortByField)
- [Some](#Some)
@@ -966,6 +968,61 @@ func main() {
### <span id="Replace">Replace</span>
<p>返回切片的副本其中前n个不重叠的old替换为new</p>
<b>函数签名:</b>
```go
func Replace[T comparable](slice []T, old T, new T, n int) []T
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b", "a", "c", "d", "a"}
fmt.Println(slice.Replace(strs, "a", "x", 0)) //{"a", "b", "a", "c", "d", "a"}
fmt.Println(slice.Replace(strs, "a", "x", 1)) //{"x", "b", "a", "c", "d", "a"}
fmt.Println(slice.Replace(strs, "a", "x", -1)) //{"x", "b", "x", "c", "d", "x"}
}
```
### <span id="ReplaceAll">ReplaceAll</span>
<p>返回切片的副本将其中old全部替换为new</p>
<b>函数签名:</b>
```go
func ReplaceAll[T comparable](slice []T, old T, new T) []T
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b", "a", "c", "d", "a"}
fmt.Println(slice.ReplaceAll(strs, "a", "x")) //{"x", "b", "x", "c", "d", "x"}
fmt.Println(slice.Replace(strs, "e", "x")) //{"a", "b", "a", "c", "d", "a"}
}
```
### <span id="Shuffle">Shuffle</span>
<p>随机打乱切片中的元素顺序</p>