diff --git a/string.go b/string.go index 7ce1e6e..670d8dd 100644 --- a/string.go +++ b/string.go @@ -227,6 +227,17 @@ func (ks *LkkString) StartsWith(str, sub string, ignoreCase bool) bool { return false } +// StartsWiths 字符串str是否以subs其中之一为开头. +func (ks *LkkString) StartsWiths(str string, subs []string, ignoreCase bool) (res bool) { + for _, sub := range subs { + res = ks.StartsWith(str, sub, ignoreCase) + if res { + break + } + } + return +} + // EndsWith 字符串str是否以sub结尾. func (ks *LkkString) EndsWith(str, sub string, ignoreCase bool) bool { if str != "" && sub != "" { @@ -237,6 +248,17 @@ func (ks *LkkString) EndsWith(str, sub string, ignoreCase bool) bool { return false } +// EndsWiths 字符串str是否以subs其中之一为结尾. +func (ks *LkkString) EndsWiths(str string, subs []string, ignoreCase bool) (res bool) { + for _, sub := range subs { + res = ks.EndsWith(str, sub, ignoreCase) + if res { + break + } + } + return +} + // Trim 去除字符串首尾处的空白字符(或者其他字符). // characterMask为要修剪的字符. func (ks *LkkString) Trim(str string, characterMask ...string) string { diff --git a/string_test.go b/string_test.go index 25d40e5..3b62f13 100644 --- a/string_test.go +++ b/string_test.go @@ -3139,4 +3139,33 @@ func BenchmarkString_CountWords(b *testing.B) { for i := 0; i < b.N; i++ { _, _ = KStr.CountWords(helloOther) } -} \ No newline at end of file +} + +func TestString_StartsWith(t *testing.T) { + var tests = []struct { + str string + sub string + ignoreCase bool + expected bool + }{ + {"", "", false, false}, + {helloEng, "", false, false}, + {helloOther2, "hello", false, false}, + {helloOther2, "Hello", false, true}, + {helloOther2, "hello", true, true}, + {helloOther2, "Hello 你好", false, true}, + {helloOther2, "hello 你好", true, true}, + {helloOther2, "world 世", true, false}, + } + for _, test := range tests { + actual := KStr.StartsWith(test.str, test.sub, test.ignoreCase) + assert.Equal(t, actual, test.expected) + } +} + +func BenchmarkString_StartsWith(b *testing.B) { + b.ResetTimer() + for i := 0; i < b.N; i++ { + KStr.StartsWith(helloOther2, "hello", true) + } +}