-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathstring.go
38 lines (35 loc) · 989 Bytes
/
string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package slice
import "strings"
// ContainsString checks if a given slice of strings contains the provided string.
// If a modifier func is provided, it is called with the slice item before the comparation.
//
// haystack := []string{"one", "Two", "Three"}
// if slice.ContainsString(haystack, "two", strings.ToLower) {
// // Do thing
// }
func ContainsString(s string, slice []string, modifier func(s string) string) bool {
for _, item := range slice {
if item == s {
return true
}
if modifier != nil && modifier(item) == s {
return true
}
}
return false
}
// ContainsStringEqualFold checks if a given slice of strings contains the provided string
// as ignore the cases.
//
// haystack := []string{"aa", "bb", "Cc"}
// if slice.ContainsStringEqualFold(haystack, "cC") {
// // Do thing
// }
func ContainsStringEqualFold(s string, slice []string) bool {
for _, item := range slice {
if strings.EqualFold(item, s) {
return true
}
}
return false
}