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, std.string;
<lang d>import std.stdio, std.array, std.algorithm;
void main()
{
auto s = `a!===b=!=c==!=d`;
auto divs = [ `==`, `!=`, `=`, `!` ];
auto lst = MultiSplit(s, divs);
foreach(int i, string p; lst) {
write(p);
if (i < lst.length-1) write(` <`~ divs[i] ~`> `);
}
}


string[] MultiSplit(string s, string[] divisors)
string[] multiSplit(in string s, string[] divisors) {
if (s.empty)
{
if (s.empty) return [];
return [];
string[] res;
string[] result;
char[] rest = s.dup;
auto rest = s.idup;

foreach(string div; divisors){
foreach (div; divisors) {
auto p = findSplit(rest, div);
auto p = findSplit(rest, div);
res.length = res.length + 1;
result.length += 1;
res[$-1] = p[0].idup;
result[$ - 1] = p[0].idup;
rest = p[2];
rest = p[2];
if (p[1].empty || rest.empty) // divisor is not found OR it was last in string
// divisor is not found OR it was last in string
if (p[1].empty || rest.empty)
break;
break;
}
}

if (!rest.empty) {
if (!rest.empty) {
res.length = res.length + 1;
result.length += 1;
res[$-1] = rest.idup;
result[$ - 1] = rest.idup;
}

return result;
}

void main() {
auto s = "a!===b=!=c==!=d";
auto divs = ["==", "!=", "=", "!"];
auto lst = multiSplit(s, divs);

foreach (i, p; lst) {
write(p);
if (i < lst.length-1)
write(" {", divs[i], "} ");
}
}
return res;
writeln();
}</lang>
}</lang>
Output (separators are in angle brackets):
Output (separators are in brackets):
<pre>a! <==> =b= <!=> c <=> = <!> =d</pre>
<pre>a! {==} =b= {!=} c {=} = {!} =d</pre>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==