Suppose I have a string myString = "ABC DEF". To split myString into a list of separate strings delimited by " ", I can simply use StringSplit:
myString = "ABC DEF";
StringSplit[myString, " "]
(* {"ABC", "DEF"} *)
Is there an analogous "splitting function" that takes a list as input, rather than a string? For example, suppose I have a list of strings:
myList = {"A", "B", "C", " ", "D", "E", "F"};
I wish to split myList at the " " to obtain two lists. The result I desire is desiredList:
desiredList = {{"A", "B", "C"}, {"D", "E", "F"}}
What function will transform myList into desiredList?
SplitBy comes close, but it retains the delimiter, " ":
SplitBy[myList, # == " " &]
(* {{"A", "B", "C"}, {" "}, {"D", "E", "F"}} *)
I can then use DeleteCases to remove the delimiter, yielding the desired result:
DeleteCases[SplitBy[myList, # == " " &], {" "}]
% == desiredList
(* {{"A", "B", "C"}, {"D", "E", "F"}} *)
(* True *)
Is there a simpler way to accomplish this?
The resource functions "SplitWhen" (ResourceFunction["SplitWhen"]) and "SplitAt" (ResourceFunction["SplitAt"]) come close, but post-processing with DeleteCases is still necessary:
ResourceFunction["SplitWhen"][myList, # == " " &]
Map[DeleteCases[#, " "] &, %]
% == desiredList
ResourceFunction["SplitAt"][myList, # == " " &, After]
Map[DeleteCases[#, " "] &, %]
% == desiredList
ResourceFunction["SplitAt"][myList, # == " " &, Before]
Map[DeleteCases[#, " "] &, %]
% == desiredList