Inspired by this StackOverflow question.
Input:
We'll take three inputs:
- A delimiter-character
D
to split on - A character
I
between two of which we ignore the delimiter character (I know, that sounds vague, but I'll explain it below) - A string
S
Output:
A list/array containing the substrings after the split.
Example:
Input:
D = ','
I = '"'
S = '11020199,"Abc ",aduz,,444,bieb,dc,2 ,2222.00,whatever 5dc,222.22,22.00,"98,00","12,000,000",21-09-2018, 06:00,",-,"'
Output:
['11020199', 'Abc ', 'aduz', '', '444', 'bieb', 'dc', '2 ', '2222.00', 'whatever 5dc', '222.22', '22.00', '98,00', '12,000,000', '21-09-2018', ' 06:00', ',-,']
Why? Splitting on comma would normally also split 98,00
, 12,000,000
and ,-,
in two/three pieces. But because they are within the I
input character, we ignore ignore the split here.
Challenge rules:
- You can assume there will always be an even amount of
I
characters in the input-string. - You can assume the character
I
will always have aD
next to it (except when it's the first or last character of the input) which can still be properly split. So you won't have something likeD = ','; I = '"'; S = 'a,b"c,d"e,f'
, nor anything like thisD=','; I='"'; S='a",b,"c'
. - The input-string
S
could contain none of eitherD
orI
. If it contains noD
, we output a list with the entire input-string as only item. - The output list won't contain the character
I
anymore, even when it contained noD
(as you can see at the"Abc "
becoming'Abc '
in the example above). - It is possible that the substring within
I
contains onlyD
. For example:D = ','; I = '"'; S = 'a,",",b,"c","d,e,,",f'
would result in['a', ',', 'b', 'c', 'd,e,,', 'f']
. - You can assume that
D
will never be at the start or end ofS
, so you won't have to deal with trailing / leading empty items. - When an input has two adjacent
D
, we'll have an empty item. I.e.D = ','; I = '"'; S = 'a,"b,c",d,,e,"",f'
would result in['a', 'b,c', 'd', '', 'e', '', 'f']
. - You can assume the inputs and outputs will only contain printable ASCII in the range
[32, 126]
(so excluding tabs and newlines). - You are also allowed to output all items new-line delimited instead of returning/outputting a list/array (especially for those languages that don't have lists/arrays; i.e. Retina).
- You are allowed to output the list in reversed order if it saves bytes. You are not allowed to output it in sorted or 'shuffled' order, though. So
D = ','; I = 'n'; S = 'a,2,b,3,c'
can be output as[a,2,b,3,c]
or[c,3,b,2,a]
, but not as[2,3,a,b,c,]
or[a,3,b,c,2]
for example.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input:
D = ','; I = '"'; S = 'a,"b,c",d,,e,"",f'
Output:
['a', 'b,c', 'd', '', 'e', '', 'f']
Input:
D = ','; I = '"'; S = '11020199,"Abc ",aduz,,444,bieb,dc,2 ,2222.00,whatever 5dc,222.22,22.00,"98,00","12,000,000",21-09-2018, 06:00,",-,"'
Output:
['11020199', 'Abc ', 'aduz', '', '444', 'bieb', 'dc', '2 ', '2222.00', 'whatever 5dc', '222.22', '22.00', '98,00', '12,000,000', '21-09-2018', ' 06:00', ',-,']
Input:
D = ' '; I = ','; S = 'this is a test , to see if you understand it, or not , hmmm, I think I have too many commas , or not , perhaps..'
Output:
['this', 'is', 'a', 'test', ' to see if you understand it', 'or', 'not', ' hmmm', 'I', 'think', 'I', 'have', 'too', 'many', 'commas', ' or not ', 'perhaps..']
Input:
D = 'x'; I = 'y'; S = 'contains no lowercase X nor Y'
Output:
['contains no lowercase X nor Y']
Input:
D = '1'; I = '3'; S = '3589841973169139975105820974944592078316406286208948254211370679314'
Output: ['58984197', '69', '9975105820974944592078', '64062862089482542', '', '70679', '4']
Input:
D = ' '; I = 'S'; S = 'regular split on spaces'
Output:
['regular', 'split', 'on', 'spaces']
"", "'ll remove all ", ""
) \$\endgroup\$D = ','; I = '"'; S = ',"a,b",c,,d,""'
I just noticed. I'll modify the challenge a bit so empty items won't be possible at the start or end. Based on experience in both Java and 05AB1E in the past, I know how frustrating empty items after split can be to fix manually when the language doesn't do it correctly by default. A test cases like this should still be supported:D = ','; I = '"'; S = 'a,"b,c",d,,e
→['a', 'b,c', 'd', '', 'e']
with an empty item in between. \$\endgroup\$'1,"2,3"' -> ['2,3','1']
\$\endgroup\$[a,b,c]
and[c,b,a]
are allowed outputs, but[a,c,b]
or[b,a,c]
not for example. \$\endgroup\$D=','; I='"'; S='a",b,"c'
or is it even a valid input? \$\endgroup\$