From 4b8b624b4c8110b645ac3ae7a1b85d5536677548 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 31 May 2023 17:27:25 +0800 Subject: [PATCH] feat: add ContainsAll and ContainsAny --- docs/strutil.md | 72 ++++++++++++++++++++++++++++++++++++++++++ docs/strutil_zh-CN.md | 71 +++++++++++++++++++++++++++++++++++++++++ strutil/string.go | 22 +++++++++++++ strutil/string_test.go | 16 ++++++++++ 4 files changed, 181 insertions(+) diff --git a/docs/strutil.md b/docs/strutil.md index f0256e8..06d342f 100644 --- a/docs/strutil.md +++ b/docs/strutil.md @@ -28,6 +28,8 @@ import ( - [BeforeLast](#BeforeLast) - [CamelCase](#CamelCase) - [Capitalize](#Capitalize) +- [ContainsAll](#ContainsAll) +- [ContainsAny](#ContainsAny) - [IsString](#IsString) - [KebabCase](#KebabCase) - [UpperKebabCase](#UpperKebabCase) @@ -1168,3 +1170,73 @@ func main() { // 1324265**** } ``` + + +### ContainsAll + +

Return true if target string contains all the substrings.

+ +Signature: + +```go +func ContainsAll(str string, substrs []string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/strutil" +) + +func main() { + str := "hello world" + + result1 := strutil.ContainsAll(str, []string{"hello", "world"}) + result2 := strutil.ContainsAll(str, []string{"hello", "abc"}) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### ContainsAny + +

Return true if target string contains any one of the substrings.

+ +Signature: + +```go +func ContainsAny(str string, substrs []string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/strutil" +) + +func main() { + str := "hello world" + + result1 := strutil.ContainsAny(str, []string{"hello", "world"}) + result2 := strutil.ContainsAny(str, []string{"hello", "abc"}) + result3 := strutil.ContainsAny(str, []string{"123", "abc"}) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` diff --git a/docs/strutil_zh-CN.md b/docs/strutil_zh-CN.md index e3bc8f8..c71fb75 100644 --- a/docs/strutil_zh-CN.md +++ b/docs/strutil_zh-CN.md @@ -27,6 +27,8 @@ import ( - [Before](#Before) - [BeforeLast](#BeforeLast) - [CamelCase](#CamelCase) +- [ContainsAll](#ContainsAll) +- [ContainsAny](#ContainsAny) - [Capitalize](#Capitalize) - [IsString](#IsString) - [KebabCase](#KebabCase) @@ -1201,3 +1203,72 @@ func main() { // 1324265**** } ``` + +### ContainsAll + +

判断字符串是否包括全部给定的子字符串切片。

+ +函数签名: + +```go +func ContainsAll(str string, substrs []string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/strutil" +) + +func main() { + str := "hello world" + + result1 := strutil.ContainsAll(str, []string{"hello", "world"}) + result2 := strutil.ContainsAll(str, []string{"hello", "abc"}) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### ContainsAny + +

判断字符串是否包括给定的子字符串切片中任意一个子字符串。

+ +函数签名: + +```go +func ContainsAny(str string, substrs []string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/strutil" +) + +func main() { + str := "hello world" + + result1 := strutil.ContainsAny(str, []string{"hello", "world"}) + result2 := strutil.ContainsAny(str, []string{"hello", "abc"}) + result3 := strutil.ContainsAny(str, []string{"123", "abc"}) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` diff --git a/strutil/string.go b/strutil/string.go index efd15e8..a6f4a93 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -481,3 +481,25 @@ func HideString(origin string, start, end int, replaceChar string) string { return startStr + replaceStr + endStr } + +// ContainsAll return true if target string contains all the substrs. +func ContainsAll(str string, substrs []string) bool { + for _, v := range substrs { + if !strings.Contains(str, v) { + return false + } + } + + return true +} + +// ContainsAny return true if target string contains any one of the substrs. +func ContainsAny(str string, substrs []string) bool { + for _, v := range substrs { + if strings.Contains(str, v) { + return true + } + } + + return false +} diff --git a/strutil/string_test.go b/strutil/string_test.go index 122234b..ee52cde 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -439,3 +439,19 @@ func TestHideString(t *testing.T) { assert.Equal("1324265****", HideString(str, 7, 100, "*")) assert.Equal("13242658976", HideString(str, 100, 100, "*")) } + +func TestContainsAll(t *testing.T) { + assert := internal.NewAssert(t, "TestContainsAll") + + assert.Equal(true, ContainsAll("hello world", []string{"hello", "world"})) + assert.Equal(true, ContainsAll("hello world", []string{""})) + assert.Equal(false, ContainsAll("hello world", []string{"hello", "abc"})) +} + +func TestContainsAny(t *testing.T) { + assert := internal.NewAssert(t, "TestContainsAny") + + assert.Equal(true, ContainsAny("hello world", []string{"hello", "world"})) + assert.Equal(true, ContainsAny("hello world", []string{"hello", "abc"})) + assert.Equal(false, ContainsAny("hello world", []string{"123", "abc"})) +}