-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
Use Case
Sometimes I encounter scenarios where I need to check if a given string contains all of a specific set of runes, such as vowels ('a', 'e', 'i', 'o', 'u'). Currently, I have to iterate through the string and runes manually.
Proposal
I propose adding a function to the strings package that can efficiently check if a string contains all of the specified runes. For example:
func containsAllRunes(s string, runes []rune) bool {
for _, r := range runes {
if ContainsRune(s, r) {
return false
}
}
return true
}
- Note that I didn't prefixed ContainsRune function with 'strings' which is the name of the package, because this function is intended to be in the strings package
- The implementation provided above is a basic example for illustration purposes.The actual implementation can be optimized for better performance and efficiency.
This function would greatly simplify such checks and improve code readability.
usage example:
func main() {
s := "hello world from Go!"
b := containsAllRunes(s, []rune("abcde"))
fmt.Println("the string contains all the provided runes:", b)
}