splitter for strings

Chris via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 9 02:52:22 PDT 2014


Say I wanna split a string that contains hyphens. If I use 
std.algorithm.splitter I end up with empty elements for each 
hyphen, e.g.:

auto word = "bla-bla";
auto parts = appender!(string[]);
w.splitter('-').copy(parts);
// parts.data.length == 3 ["bla", "", "bla"]

This is not ideal for my purposes, so I filter like so:

auto parts = appender!(string[]);
foreach (p; word.splitter('-')) {
   if (p != "") {
     parts ~= p;
   }
}

or even better like so:

w.splitter('-').filter!(a => a != "").copy(parts);

I wonder, however, whether this is ideal or whether regex's split 
would be a better match (pardon the pun!). I try to avoid regex 
when ever possible since they are more awkward to use and usually 
more expensive.


More information about the Digitalmars-d-learn mailing list