www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to split a string/array with multiple separators?

reply Borislav Kosharov <boby_dsm abv.bg> writes:
I want to split a string using multiple separators. In std.array 
the split function has a version where it takes a range as a 
separator, but it works differently than what I want. Say if I 
call it with " -> " it will search for the whole thing together. 
I want to pass split a list of separators say [":", ",", ";"] and 
if it finds any of those to split it.

Sorry if this questions is stupid but I cant find how to do it.
Dec 16 2015
parent reply Dragos Carp <dragoscarp gmail.com> writes:
On Wednesday, 16 December 2015 at 14:18:28 UTC, Borislav Kosharov 
wrote:
 I want to split a string using multiple separators. In 
 std.array the split function has a version where it takes a 
 range as a separator, but it works differently than what I 
 want. Say if I call it with " -> " it will search for the whole 
 thing together. I want to pass split a list of separators say 
 [":", ",", ";"] and if it finds any of those to split it.

 Sorry if this questions is stupid but I cant find how to do it.
void main() { import std.stdio: writeln; writeln("abc,def;ghi".splitter!(a => !":,;".find(a).empty).array); }
Dec 16 2015
parent Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Wednesday, 16 December 2015 at 14:47:26 UTC, Dragos Carp wrote:
 On Wednesday, 16 December 2015 at 14:18:28 UTC, Borislav 
 Kosharov wrote:
 I want to split a string using multiple separators. In 
 std.array the split function has a version where it takes a 
 range as a separator, but it works differently than what I 
 want. Say if I call it with " -> " it will search for the 
 whole thing together. I want to pass split a list of 
 separators say [":", ",", ";"] and if it finds any of those to 
 split it.

 Sorry if this questions is stupid but I cant find how to do it.
void main() { import std.stdio: writeln; writeln("abc,def;ghi".splitter!(a => !":,;".find(a).empty).array); }
The call to `array` is unnecessary in this example, and you can use the shorter `canFind`: writeln("abc,def;ghi".splitter!(a => ":,;".canFind(a)));
Dec 16 2015