mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-12 08:42:29 +08:00
doc: add docment for Replace and ReplaceAll
This commit is contained in:
@@ -53,6 +53,8 @@ import (
|
|||||||
- [Map](#Map)
|
- [Map](#Map)
|
||||||
- [Reverse](#Reverse)
|
- [Reverse](#Reverse)
|
||||||
- [Reduce](#Reduce)
|
- [Reduce](#Reduce)
|
||||||
|
- [Replace](#Replace)
|
||||||
|
- [ReplaceAll](#ReplaceAll)
|
||||||
- [Shuffle](#Shuffle)
|
- [Shuffle](#Shuffle)
|
||||||
- [SortByField](#SortByField)
|
- [SortByField](#SortByField)
|
||||||
- [Some](#Some)
|
- [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>
|
### <span id="Shuffle">Shuffle</span>
|
||||||
<p>Creates an slice of shuffled values.</p>
|
<p>Creates an slice of shuffled values.</p>
|
||||||
|
|||||||
@@ -53,6 +53,8 @@ import (
|
|||||||
- [Map](#Map)
|
- [Map](#Map)
|
||||||
- [Reverse](#Reverse)
|
- [Reverse](#Reverse)
|
||||||
- [Reduce](#Reduce)
|
- [Reduce](#Reduce)
|
||||||
|
- [Replace](#Replace)
|
||||||
|
- [ReplaceAll](#ReplaceAll)
|
||||||
- [Shuffle](#Shuffle)
|
- [Shuffle](#Shuffle)
|
||||||
- [SortByField](#SortByField)
|
- [SortByField](#SortByField)
|
||||||
- [Some](#Some)
|
- [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>
|
### <span id="Shuffle">Shuffle</span>
|
||||||
<p>随机打乱切片中的元素顺序</p>
|
<p>随机打乱切片中的元素顺序</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user