Multisplit: Difference between revisions
Content added Content deleted
(Cleaned up D version) |
|||
Line 8: | Line 8: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio, std.array, std.algorithm |
<lang d>import std.stdio, std.array, std.algorithm; |
||
⚫ | |||
{ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
string[] |
string[] multiSplit(in string s, string[] divisors) { |
||
if (s.empty) |
|||
{ |
|||
return []; |
|||
string[] |
string[] result; |
||
auto rest = s.idup; |
|||
foreach |
foreach (div; divisors) { |
||
auto p = findSplit(rest, div); |
auto p = findSplit(rest, div); |
||
result.length += 1; |
|||
result[$ - 1] = p[0].idup; |
|||
rest = p[2]; |
rest = p[2]; |
||
// divisor is not found OR it was last in string |
|||
if (p[1].empty || rest.empty) |
|||
break; |
break; |
||
} |
} |
||
if (!rest.empty) { |
if (!rest.empty) { |
||
result.length += 1; |
|||
result[$ - 1] = rest.idup; |
|||
⚫ | |||
return result; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
if (i < lst.length-1) |
|||
⚫ | |||
} |
} |
||
writeln(); |
|||
}</lang> |
}</lang> |
||
Output (separators are in |
Output (separators are in brackets): |
||
<pre>a! |
<pre>a! {==} =b= {!=} c {=} = {!} =d</pre> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |