diff --git a/strings/stringsutil.go b/strings/stringsutil.go index b98b047..c8e6440 100644 --- a/strings/stringsutil.go +++ b/strings/stringsutil.go @@ -130,7 +130,7 @@ func Reverse(s string) string { return string(rune) } -// ContainsAny returns true is s contains any specified substring +// ContainsAny returns true if s contains any specified substring. func ContainsAny(s string, ss ...string) bool { for _, sss := range ss { if strings.Contains(s, sss) { @@ -140,6 +140,17 @@ func ContainsAny(s string, ss ...string) bool { return false } +// ContainsAnyI returns true if s contains any specified substring (case-insensitive). +func ContainsAnyI(s string, ss ...string) bool { + s = strings.ToLower(s) + for _, sss := range ss { + if strings.Contains(s, strings.ToLower(sss)) { + return true + } + } + return false +} + // EqualFoldAny returns true if s is equal to any specified substring func EqualFoldAny(s string, ss ...string) bool { for _, sss := range ss { diff --git a/strings/stringsutil_test.go b/strings/stringsutil_test.go index 6ebee44..a9069e2 100644 --- a/strings/stringsutil_test.go +++ b/strings/stringsutil_test.go @@ -199,6 +199,20 @@ func TestContainsAny(t *testing.T) { } } +func TestContainsAnyI(t *testing.T) { + tests := map[string]containstest{ + "abc": {Items: []string{"A", "b"}, Result: true}, + "abcd": {Items: []string{"X", "b"}, Result: true}, + "A b C": {Items: []string{"X"}, Result: false}, + "aaa": {Items: []string{"A"}, Result: true}, + "Hello!": {Items: []string{"hELLO", "world"}, Result: true}, + } + for str, test := range tests { + res := ContainsAnyI(str, test.Items...) + require.Equalf(t, test.Result, res, "test: %+v", res) + } +} + func TestEqualFoldAny(t *testing.T) { tests := map[string]containstest{ "abc": {Items: []string{"a", "Abc"}, Result: true},