diff --git a/strings/stringsutil.go b/strings/stringsutil.go index 6698754..4b71999 100644 --- a/strings/stringsutil.go +++ b/strings/stringsutil.go @@ -302,3 +302,13 @@ func IndexAny(s string, seps ...string) (int, string) { } return -1, "" } + +// ContainsAll returns true if s contains all specified substrings. +func ContainsAll(s string, ss ...string) bool { + for _, sub := range ss { + if !strings.Contains(s, sub) { + return false + } + } + return true +} diff --git a/strings/stringsutil_test.go b/strings/stringsutil_test.go index 9d0dbab..c563fc0 100644 --- a/strings/stringsutil_test.go +++ b/strings/stringsutil_test.go @@ -390,3 +390,20 @@ func TestIndexAny(t *testing.T) { require.Equal(t, test.expectedSep, sep) } } + +func TestContainsAll(t *testing.T) { + tests := []struct { + s string + ss []string + result bool + }{ + {"abcdefg", []string{"a", "b"}, true}, + {"abcdefg", []string{"a", "z"}, false}, + {"abcdefg", []string{"a", "b", "c", "d", "e", "f", "g"}, true}, + {"abcdefg", []string{"a", "b", "c", "d", "e", "f", "g", "z"}, false}, + } + for _, test := range tests { + res := ContainsAll(test.s, test.ss...) + require.Equal(t, test.result, res) + } +}