[Tex/LaTex] Splitting Strings by Comma


I need to split a string by commas, and I haven't found an easy way to do it in xstring or xparse (although I may be missing something obvious). So if I say:

\def\pleaseHelp{"I, am, confused"}

What is the simplest way to extract:

{"I", "am", "confused"}

from \pleaseHelp and store it as a variable? I feel like this should be simple.

Best Answer

listofitems is a powerful list parsing package. Your example is the simplest of its capability:

\def\pleaseHelp{I, am, confused}
\readlist*\mylist{\pleaseHelp}% star option removes surrounding whitespace
Individual items:\\
``\mylist[1]'' and\\
``\mylist[2]'' and\\

Loop over list:
\foreachitem\x\in\mylist[]{\ifnum\xcnt=1\else\ and \fi``\x''}

enter image description here

Additionally, you can change the the parsing separator with the following prior to the \readlist:


You can parse based on several separators simultaneously with an or (||) separated specification of separators:

\readlist*\Z{To be, or not to be; that is the question.}

will yield a list containing \Z[1] as To be, \Z[2] as or not to be, \Z[3] as that is the question.

You can do nested parsing with a slash (/) separated list of separators, for example:

\readlist*\Z{this is, a test* of multi-level, parsing}

Then, \Z[1,1] contains this is, \Z[1,2] contains a test, \Z[2,1] contains of multi-level, and \Z[2,2] contains parsing.

When I use the word "contains", I mean "expands to the actual tokens, via two expansions" so that, in the above example, \detokenize\expandafter\expandafter\expandafter{\Z[2,2]} will yield parsing.

Related Question