Split Text by Multiple Delimiters
Problem statement
You are given a string text and a string delimiters containing different separator characters. Split text wherever any character from delimiters appears.
Return the nonempty pieces in their original order. Treat delimiters literally, including characters that have special meanings in regular expressions. Do not trim spaces, change case, or otherwise change the contents of a piece. A space separates pieces only when it is itself a delimiter.
An empty delimiter string makes no cuts; an empty input string produces no tokens.
Function
splitByDelimiters(text: String, delimiters: String) → String[]Examples
Example 1
text = "red,green;blue"delimiters = ",;"return = ["red","green","blue"]Both comma and semicolon divide the text.
Example 2
text = ";a,,b;"delimiters = ",;"return = ["a","b"]Leading, repeated and trailing delimiters do not create empty output tokens.
Example 3
text = "A.B+C"delimiters = ".+"return = ["A","B","C"]The period and plus sign are literal separators, with no regular-expression meaning.
Constraints
0 <= text.length <= 10000.0 <= delimiters.length <= 95; delimiter characters are distinct.- Both strings contain printable ASCII characters, from code point
32through126.