github.com/mailgun/holster/v4@v4.20.0/slice/string.go (about) 1 package slice 2 3 import "strings" 4 5 // ContainsString checks if a given slice of strings contains the provided string. 6 // If a modifier func is provided, it is called with the slice item before the comparation. 7 // 8 // haystack := []string{"one", "Two", "Three"} 9 // if slice.ContainsString(haystack, "two", strings.ToLower) { 10 // // Do thing 11 // } 12 func ContainsString(s string, slice []string, modifier func(s string) string) bool { 13 for _, item := range slice { 14 if item == s { 15 return true 16 } 17 if modifier != nil && modifier(item) == s { 18 return true 19 } 20 } 21 return false 22 } 23 24 // ContainsStringEqualFold checks if a given slice of strings contains the provided string 25 // as ignore the cases. 26 // 27 // haystack := []string{"aa", "bb", "Cc"} 28 // if slice.ContainsStringEqualFold(haystack, "cC") { 29 // // Do thing 30 // } 31 func ContainsStringEqualFold(s string, slice []string) bool { 32 for _, item := range slice { 33 if strings.EqualFold(item, s) { 34 return true 35 } 36 } 37 return false 38 }