diff --git a/docs/api/packages/strutil.md b/docs/api/packages/strutil.md index db068a4..869c893 100644 --- a/docs/api/packages/strutil.md +++ b/docs/api/packages/strutil.md @@ -65,6 +65,7 @@ import ( - [Concat](#Concat) - [Ellipsis](#Ellipsis) - [Shuffle](#Shuffle) +- [Rotate](#Rotate)
@@ -1553,17 +1554,17 @@ import ( func main() { - result1 := strutil.Concat(12, "Hello", " ", "World", "!") - result2 := strutil.Concat(11, "Go", " ", "Language") - result3 := strutil.Concat(0, "An apple a ", "day,", "keeps the", " doctor away") - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) + result1 := strutil.Concat(12, "Hello", " ", "World", "!") + result2 := strutil.Concat(11, "Go", " ", "Language") + result3 := strutil.Concat(0, "An apple a ", "day,", "keeps the", " doctor away") + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - // Output: - // Hello World! - // Go Language - // An apple a day,keeps the doctor away + // Output: + // Hello World! + // Go Language + // An apple a day,keeps the doctor away } ``` @@ -1623,4 +1624,38 @@ func main() { result := strutil.Shuffle("hello") fmt.Println(result) //olelh (random order) } +``` + +### Rotate + +

打乱给定字符串中的字符顺序。

+ +函数签名: + +```go +func Rotate(str string, shift int) string +``` + +示例:[运行]() + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + result1 := Rotate("Hello", 0) + result2 := Rotate("Hello", 1) + result3 := Rotate("Hello", 2) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // Hello + // oHell + // loHel +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/strutil.md b/docs/en/api/packages/strutil.md index 3dfaad1..de26035 100644 --- a/docs/en/api/packages/strutil.md +++ b/docs/en/api/packages/strutil.md @@ -65,6 +65,7 @@ import ( - [Concat](#Concat) - [Ellipsis](#Ellipsis) - [Shuffle](#Shuffle) +- [Rotate](#Rotate)
@@ -1625,4 +1626,37 @@ func main() { result := strutil.Shuffle("hello") fmt.Println(result) //olelh (random order) } -``` \ No newline at end of file +``` + +### Rotate + +

Rotates the string by the specified number of characters.

+ +Signature: + +```go +func Rotate(str string, shift int) string +``` + +Example:[Run]() + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + result1 := Rotate("Hello", 0) + result2 := Rotate("Hello", 1) + result3 := Rotate("Hello", 2) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // Hello + // oHell + // loHel +} \ No newline at end of file diff --git a/strutil/string.go b/strutil/string.go index 4142767..981cf54 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -676,3 +676,31 @@ func Shuffle(str string) string { return string(runes) } + +// Rotate rotates the string by the specified number of characters. +// Play: todo +func Rotate(str string, shift int) string { + if shift == 0 { + return str + } + + runes := []rune(str) + length := len(runes) + if length == 0 { + return str + } + + shift = shift % length + + if shift < 0 { + shift = length + shift + } + + var sb strings.Builder + sb.Grow(length) + + sb.WriteString(string(runes[length-shift:])) + sb.WriteString(string(runes[:length-shift])) + + return sb.String() +} diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index 514e904..fe81f5e 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -709,3 +709,18 @@ func ExampleEllipsis() { // 你好... // 😀😃😄... } + +func ExampleRotate() { + result1 := Rotate("Hello", 0) + result2 := Rotate("Hello", 1) + result3 := Rotate("Hello", 2) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // Hello + // oHell + // loHel +} diff --git a/strutil/string_test.go b/strutil/string_test.go index f06dcd6..3ad4bc7 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -710,9 +710,9 @@ func TestEllipsis(t *testing.T) { assert := internal.NewAssert(t, "TestEllipsis") tests := []struct { - input string - length int - want string + input string + length int + expected string }{ {"", 0, ""}, {"hello world", 0, ""}, @@ -725,7 +725,7 @@ func TestEllipsis(t *testing.T) { } for _, tt := range tests { - assert.Equal(tt.want, Ellipsis(tt.input, tt.length)) + assert.Equal(tt.expected, Ellipsis(tt.input, tt.length)) } } @@ -741,3 +741,28 @@ func TestShuffle(t *testing.T) { shuffledStr := Shuffle(str) assert.Equal(5, len(shuffledStr)) } + +func TestRotate(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestRotate") + + tests := []struct { + input string + shift int + expected string + }{ + {"", 1, ""}, + {"a", 0, "a"}, + {"a", 1, "a"}, + {"a", -1, "a"}, + + {"Hello", -2, "lloHe"}, + {"Hello", 1, "oHell"}, + {"Hello, world!", 3, "ld!Hello, wor"}, + } + + for _, tt := range tests { + assert.Equal(tt.expected, Rotate(tt.input, tt.shift)) + } +}