diff --git a/docs/api/packages/strutil.md b/docs/api/packages/strutil.md index c2cc4e6..5dc5058 100644 --- a/docs/api/packages/strutil.md +++ b/docs/api/packages/strutil.md @@ -69,6 +69,7 @@ import ( - [TemplateReplace](#TemplateReplace) - [RegexMatchAllGroups](#RegexMatchAllGroups) - [ExtractContent](#ExtractContent) +- [FindAllOccurrences](#FindAllOccurrences)
@@ -1708,7 +1709,7 @@ func main() { func RegexMatchAllGroups(pattern, str string) [][]string ``` -示例:[Run](https://go.dev/play/p/JZiu0RXpgN-) +示例:[运行](https://go.dev/play/p/JZiu0RXpgN-) ```go import ( @@ -1741,7 +1742,7 @@ func main() { func ExtractContent(s, start, end string) []string ``` -示例:[Run](https://go.dev/play/p/Ay9UIk7Rum9) +示例:[运行](https://go.dev/play/p/Ay9UIk7Rum9) ```go import ( @@ -1759,4 +1760,32 @@ func main() { // Output: // [content1 content2 content1] } +``` + +### FindAllOccurrences + +

返回子字符串在字符串中所有出现的位置。

+ +函数签名: + +```go +func FindAllOccurrences(str, substr string) []int +``` + +示例:[运行](todo) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + result := strutil.FindAllOccurrences("ababab", "ab") + + fmt.Println(result) + + // Output: + // [0 2 4] +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/strutil.md b/docs/en/api/packages/strutil.md index 3e98521..28c7ef8 100644 --- a/docs/en/api/packages/strutil.md +++ b/docs/en/api/packages/strutil.md @@ -68,7 +68,8 @@ import ( - [Rotate](#Rotate) - [TemplateReplace](#TemplateReplace) - [RegexMatchAllGroups](#RegexMatchAllGroups) -- [ExtractContent](#RegexMatchAllGroups) +- [ExtractContent](#ExtractContent) +- [FindAllOccurrences](#FindAllOccurrences)
@@ -1761,4 +1762,32 @@ func main() { // Output: // [content1 content2 content1] } +``` + +### FindAllOccurrences + +

Returns the positions of all occurrences of a substring in a string.

+ +Signature: + +```go +func FindAllOccurrences(str, substr string) []int +``` + +Example:[Run](todo) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + result := strutil.FindAllOccurrences("ababab", "ab") + + fmt.Println(result) + + // Output: + // [0 2 4] +} ``` \ No newline at end of file diff --git a/strutil/string.go b/strutil/string.go index 1f614cd..d33522d 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -738,14 +738,14 @@ func RegexMatchAllGroups(pattern, str string) [][]string { // ExtractContent extracts the content between the start and end strings in the source string. // Play: https://go.dev/play/p/Ay9UIk7Rum9 -func ExtractContent(s, start, end string) []string { +func ExtractContent(str, start, end string) []string { result := []string{} for { - if _, after, ok := strings.Cut(s, start); ok { + if _, after, ok := strings.Cut(str, start); ok { if before, _, ok := strings.Cut(after, end); ok { result = append(result, before) - s = after + str = after } else { break } @@ -756,3 +756,20 @@ func ExtractContent(s, start, end string) []string { return result } + +// FindAllOccurrences returns the positions of all occurrences of a substring in a string. +// Play: todo +func FindAllOccurrences(str, substr string) []int { + var positions []int + + for i := 0; i < len(str); { + index := strings.Index(str[i:], substr) + if index == -1 { + break + } + positions = append(positions, i+index) + i += index + 1 + } + + return positions +} diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index 00eb7c1..a8bf402 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -763,5 +763,13 @@ func ExampleExtractContent() { // Output: // [content1 content2 content1] - +} + +func ExampleFindAllOccurrences() { + result := FindAllOccurrences("ababab", "ab") + + fmt.Println(result) + + // Output: + // [0 2 4] } diff --git a/strutil/string_test.go b/strutil/string_test.go index 79bd14c..dd0a4af 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -937,3 +937,32 @@ func TestExtractContent(t *testing.T) { }) } } + +func TestFindAllOccurrences(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestFindAllOccurrences") + + var empty []int + tests := []struct { + input string + substr string + expected []int + }{ + {"", "", empty}, + {"", "a", empty}, + {"a", "", []int{0}}, + {"a", "a", []int{0}}, + {"aa", "a", []int{0, 1}}, + {"ababab", "ab", []int{0, 2, 4}}, + {"ababab", "ba", []int{1, 3}}, + {"ababab", "c", empty}, + {"ababab", "ababab", []int{0}}, + {"ababab", "abababc", empty}, + {"ababab", "abababa", empty}, + } + + for _, tt := range tests { + assert.Equal(tt.expected, FindAllOccurrences(tt.input, tt.substr)) + } +}