-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
Proposal Details
The strings package contains the function Split that splits a string whereever the separator string occurs. Only one string can be specified.
There are use cases where one wants to split on any of a collection of characters. Often FieldsFunc is recommended for this. However, FieldsFunc has a bug in that it skips leading and trailing separators. This behaviour can not be fixed, just documented.
In order to make it possible to split a string on any of several characters there should be functions analogous to IndexAny, namely SplitAny and CountAny.
SplitAny would have the signature func SplitAny(s, chars string) []string, while CountAny would be func CountAny(s, chars string) int.
SplitAny splits the string on any character in chars, while CountAny returns how many times any of the characters in chars appears in the supplied string.
There could be another function SplitAnyN with the signature func SplitAnyN(s, chars string, n int []string that limits the splitting to a maximum of n strings.
I attach file split_any.zip where SplitAny and CountAny have been implemented as an example.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status