Skip to content

proposal: strings: function to check if a string contains all specified runes #63328

@hrtsegv

Description

@hrtsegv

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)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions