63 lines
2.3 KiB
Text
63 lines
2.3 KiB
Text
{{task heading}}
|
|
Write a function or program that can split a string at each non-escaped occurrence of a separator character.
|
|
|
|
It should accept three input parameters:
|
|
:::* The <b>string</b>
|
|
:::* The <b>separator character</b>
|
|
:::* The <b>escape character</b>
|
|
|
|
<br>
|
|
It should output a list of strings.
|
|
|
|
{{task heading|Details}}
|
|
|
|
Rules for splitting:
|
|
* The fields that were separated by the separators, become the elements of the output list.
|
|
* Empty fields should be preserved, even at the start and end.
|
|
|
|
<br>
|
|
Rules for escaping:
|
|
* "Escaped" means preceded by an occurrence of the escape character that is not already escaped itself.
|
|
* When the escape character precedes a character that has no special meaning, it still counts as an escape (but does not do anything special).
|
|
* Each occurrence of the escape character that was used to escape something, should '''not''' become part of the output.
|
|
|
|
|
|
{{task heading|Test case}}
|
|
Demonstrate that your function satisfies the following test-case:
|
|
{| class="wikitable"
|
|
|-
|
|
! Input
|
|
! Output
|
|
|-
|
|
| style="vertical-align:top" |
|
|
{| style="border-collapse:collapse; border:none" border="0"
|
|
|-
|
|
| style="border:none; text-align:right" | string:
|
|
| style="border:none" | <pre style="display:inline;padding:0.1em;margin:0.3em;">one^|uno||three^^^^|four^^^|^cuatro|</pre>
|
|
|-
|
|
| style="border:none; text-align:right" | separator character:
|
|
| style="border:none" | <pre style="display:inline;padding:0.1em;margin:0.3em;">|</pre>
|
|
|-
|
|
| style="border:none; text-align:right" | escape character:
|
|
| style="border:none" | <pre style="display:inline;padding:0.1em;margin:0.3em;">^</pre>
|
|
|}
|
|
|
|
|
{| style="border-collapse:collapse; border:none" border="0"
|
|
|-
|
|
| style="border:none" | <pre style="display:inline;padding:0.1em;margin:0.3em;">one|uno</pre>
|
|
|-
|
|
| style="border:none" | <pre style="display:inline;padding:0.1em;margin:0.3em;"></pre>
|
|
|-
|
|
| style="border:none" | <pre style="display:inline;padding:0.1em;margin:0.3em;">three^^</pre>
|
|
|-
|
|
| style="border:none" | <pre style="display:inline;padding:0.1em;margin:0.3em;">four^|cuatro</pre>
|
|
|-
|
|
| style="border:none" | <pre style="display:inline;padding:0.1em;margin:0.3em;"></pre>
|
|
|}
|
|
|}
|
|
|
|
(Print the output list in any format you like, as long as it is it easy to see what the fields are.)
|
|
|
|
{{Template:Strings}}
|
|
<br><br>
|
|
|