% !TeX TS-program = lualatex \documentclass[english,a4paper,10pt]{article} \usepackage{fontspec} \usepackage[bookmarks=true,bookmarksopen=true,colorlinks=true,hyperfootnotes=false,filecolor=black,linkcolor=blue,urlcolor=blue,pdfauthor={Christian Tellechea},pdftitle={xstring},pdfsubject={package for strings of tokens},pdfkeywords={xstring,latex,string},pdfcreator={LaTeX}]{hyperref} \usepackage[margin=1.9cm]{geometry} \usepackage{libertine} \usepackage[scale=0.85]{noto-mono} \usepackage[bottom]{footmisc} \usepackage{amsmath,amssymb,amsfonts,xcolor,eurosym,xspace,xstring,babel} \makeatletter \newcommand\make@car@active[1]{% \catcode`#1\active \begingroup \lccode`\~`#1\relax \lowercase{\endgroup\def~}% } \newcount\code@line \newbox\box@code \newbox\box@exec \newbox\box@bkgd \newcommand*\showcode{% \begingroup \parskip\z@ \parindent\z@ \code@line\z@ \@makeother\;\@makeother\!\@makeother\?\@makeother\: \medbreak \noindent \begingroup \let\do\@makeother\dospecials \make@car@active\ { }% \make@car@active\^^M{\par\leavevmode}% \make@car@active\^^I{\space\space\space\space}% \make@car@active\,{\leavevmode\kern\z@\string,}% \make@car@active\-{\leavevmode\kern\z@\string-}% \showcode@a } \newcommand\showcode@a[2][65]{% \def\@tempa##1#2{% \xdef\the@code{##1}% \endgroup \showcode@b{#1}{##1}}% \@tempa} \newcommand\showcode@b[2]{% \vskip0.5ex \fboxsep5pt \setbox\box@code=\hbox{% $\vcenter{ \hsize\dimexpr0.#1\linewidth-\fboxsep-\fboxrule\relax \parskip0pt \ttfamily \small \everypar{\llap{\global\advance\code@line\@ne\tiny\number\code@line\kern\dimexpr\fboxsep+3pt }}% \the@code\par }$% }% \setbox\box@exec=\hbox{% $\vcenter{ \hsize\dimexpr\linewidth-0.#1\linewidth-\fboxsep-\fboxrule\relax \small \newlinechar`\^^M\everyeof{\noexpand}% \scantokens{#2}% }$% }% \fbox{% \begingroup \color{black!15}% \setbox\box@bkgd=\hbox{% fond code \vrule width\wd\box@code height\dimexpr\max@dim{\ht\box@code}{\ht\box@exec}+\fboxsep\relax depth \dimexpr\max@dim{\dp\box@code}{\dp\box@exec}+\fboxsep\relax }% \wd\box@bkgd=0pt \ht\box@bkgd=0pt \dp\box@bkgd=0pt \kern-\fboxsep \box\box@bkgd \kern\fboxsep \endgroup \setbox\box@bkgd=\hbox{% réglure verticale \vrule width.5pt height\dimexpr\max@dim{\ht\box@code}{\ht\box@exec}+\fboxsep\relax depth \dimexpr\max@dim{\dp\box@code}{\dp\box@exec}+\fboxsep\relax }% \box\box@code \wd\box@bkgd=0pt \ht\box@bkgd=0pt \dp\box@bkgd=0pt \kern-\fboxsep \box\box@bkgd \kern\fboxsep \box\box@exec }% \medbreak \endgroup } \def\max@dim#1#2{\ifdim#1>#2 #1\else#2\fi} \definecolor{violet}{rgb}{0.66,0,0.66} \makeatother \newcommand\guill[1]{"#1"} \newcommand\argu[1]{$\langle$\textit{#1}$\rangle$} \newcommand\ARGU[1]{\texttt{\{}\argu{#1}\texttt{\}}} \newcommand\arguC[1]{\texttt{[}\argu{#1}\texttt{]}} \newcommand\arguCC[2]{\texttt{[}\argu{#1}{,}\argu{#2}\texttt{]}} \newcommand\texte[1]{\texttt{text}${}_{#1}$} \newcommand\etoile{$\langle$\texttt{[*]}$\rangle$} \newenvironment{Conditions}[1][1cm]% {\begin{list}% {$\vartriangleright$}% {\setlength{\leftmargin}{#1} \setlength{\itemsep}{0pt} \setlength{\parsep}{0pt} \setlength{\topsep}{2ptplus3ptminus2pt} }}% {\end{list}} \renewcommand\th{${}^\text{th}$\xspace} \newcommand\US{syntax unit\xspace} \newcommand\USs{syntax units\xspace} \newcommand\Xstring{\textsf{xstring}\xspace} \newcommand\styleexemple{\small\baselineskip1.03\baselineskip\vskip\baselineskip\relax} \newcommand\styleexercice{\footnotesize} \newcommand\colorise{\color{violet}} \def\xstringenglishdateaux#1/#2/#3\nil{\number#3\relax\space \ifcase #2 \or January\or February\or March\or April\or May\or June\or July\or August\or September\or October\or November\or December\fi\space#1} \edef\xstringenglishdate{\expandafter\xstringenglishdateaux\xstringdate\nil} \begin{document} \setlength{\parindent}{0pt} \begin{titlepage} \null\par\vfill \begin{center} \begin{minipage}{0.75\linewidth} \begin{center} \Huge\bfseries xstring\par\vspace{5pt} \small v\xstringversion\par\vspace{25pt} \normalsize User's manual \end{center} \end{minipage} \end{center} \vspace{1cm} \begin{center} Christian {\sc Tellechea}\par\small \href{mailto:unbonpetit@netc.fr}{\nolinkurl{unbonpetit@netc.fr}}\par\vspace{5pt} \xstringenglishdate \end{center} \vfill\hrulefill \begin{center} \begin{minipage}{0.85\linewidth} \noindent \hfill\textbf{\textit{Abstract}}\hfill{}\medskip\par This package provides non expandable macros for manipulating code made of tokens. For a basic use, tokens can be alphanumeric chars, but the macros can also be useful for manipulating tokens, i.e. \TeX{} code. \end{minipage} \end{center} \hrulefill\vfill{} \end{titlepage} \tableofcontents This manual is a translation of the french manual. I apologize for my poor english but I did my best\footnote{Any email to tell me errors would be appreciated!}, and I hope that the following is comprehensible! \section{Presentation} This extension\footnote{This extension does not require \LaTeX{} and can be compiled with Plain $\varepsilon$-\TeX{}.} provides non expandable macros and tests operating on "strings of tokens", as other programmation languages have. They provides the usual strings operations, such as: test if a string contains another, begins or ends with another, extractions of strings, calculation of the position of a substring, of the number of occurrences, etc.\medskip A "string of tokens" is a list of tokens of any nature, except that braces must be balanced and tokens catcode 6 and 14 (usualy \verb|%| et \verb|#|) are not allowed. Apart from this, any token is allowed (including \verb|\par|), in any order in the list, whatever be the resulting code.\medskip \Xstring reads the arguments of the macros \US by \US\footnote{In the \TeX{} code, a \US is a control sequence, a group between brace or a single char. See also page~\pageref{developpementarguments}.} : when \USs are \guill{simple} chars (catcode 10, 11 and 12), \Xstring logically read the argument char by char. \Xstring can also be used for programming purpose, including in arguments other tokens such as control sequences, braces and tokens with other catcodes. See chapter on reading mode and arguments expansion (page~\pageref{developpementarguments}), the command \verb|\verbtocs| (page~\pageref{verbtocs}) and the command \verb|\scancs| (page~\pageref{scancs}).\medskip As the arguments may contain any token, advanced users could have problems with catcodes leading to unexpected behaviours. These behaviours can be controlled: read page~\pageref{macrosetoilees}.\medskip Certainly, other packages exist (for example \href{http://mirror.ctan.org/tex-archive/macros/latex/contrib/substr/}{\nolinkurl{substr}} and \href{http://mirror.ctan.org/tex-archive/macros/latex/contrib/stringstrings/}{\nolinkurl{stringstrings}}), but as well as differences on features, they do not take into account occurrences so I found them too limited and difficult to use for programming. \section{The macros} For a better understanding, let's see first the macros with the simpler arguments possible. No special catcode, no exotic token, no control sequence neither: only alphanumeric chars will be contained in the arguments.\medskip In the following chapters, all the macros will be presented this plan: \begin{itemize} \item the syntax\footnote{The optional star, the optional argument in last position will be explained later. See page~\pageref{macrosetoilees} for starred macros and page~\pageref{argumentoptionnel} for the optional argument.} and the value of optional arguments \item a short description of the operation; \item the operation under special conditions. For each conditions considered, the operation described has priority on that (those) below; \item finally, several examples\footnote{For much more examples, see the test file.} are given. I tried to find them most easily comprehensible and most representative of the situations met in normal use. If a doubt is possible with spaces in the result, this one will be delimited by \guill{|}, given that an empty string is represented by \guill{||}. \end{itemize}\smallskip \textbf{Important}: in the following, a \argu{number} can be an integer written with numeric chars, a counter, or the result of an arithmetic operation made with the command \verb|\numexpr|.\smallskip \subsection{The tests} \subsubsection{\ttfamily\textbackslash IfSubStr} \verb|\IfSubStr|\etoile\arguC{number}\ARGU{string}\ARGU{stringA}\ARGU{true}\ARGU{false} \smallskip The value of the optional argument \argu{number} is 1 by default.\par\smallskip Tests if \argu{string} contains at least \argu{number} times \argu{stringA} and runs \argu{true} if so, and \argu{false} otherwise.\medskip \begin{Conditions} \item If \argu{number}${}\leqslant0$, runs \argu{false}; \item If \argu{string} or \argu{stringA} is empty, runs \argu{false}. \end{Conditions} \showcode|\IfSubStr{xstring}{tri}{true}{false}\par \IfSubStr{xstring}{a}{true}{false}\par \IfSubStr{a bc def }{c d}{true}{false}\par \IfSubStr{a bc def }{cd}{true}{false}\par \IfSubStr[2]{1a2a3a}{a}{true}{false}\par \IfSubStr[3]{1a2a3a}{a}{true}{false}\par \IfSubStr[4]{1a2a3a}{a}{true}{false}| \subsubsection{\ttfamily\textbackslash IfSubStrBefore} \verb|\IfSubStrBefore|\etoile\arguCC{number1}{number2}\ARGU{string}\ARGU{stringA}\ARGU{stringB}\ARGU{true}\ARGU{false} \smallskip The values of the optional arguments \argu{number1} and \argu{number2} are 1 by default.\par\smallskip In \argu{string}, tests if the \argu{number1}\th occurrence of \argu{stringA} is on the left of the \argu{number2}\th occurrence of \argu{stringB}. Runs \argu{true} if so, and \argu{false} otherwise.\medskip \begin{Conditions} \item If one of the occurrences is not found, it runs \argu{false}; \item If one of the arguments \argu{string}, \argu{stringA} or \argu{stringB} is empty, runs \argu{false}; \item If one of the optional arguments is negative or zero, runs \argu{false}. \end{Conditions} \showcode|\IfSubStrBefore{xstring}{st}{in}{true}{false}\par \IfSubStrBefore{xstring}{ri}{s}{true}{false}\par \IfSubStrBefore{LaTeX}{LaT}{TeX}{true}{false}\par \IfSubStrBefore{a bc def }{ b}{ef}{true}{false}\par \IfSubStrBefore{a bc def }{ab}{ef}{true}{false}\par \IfSubStrBefore[2,1]{b1b2b3}{b}{2}{true}{false}\par \IfSubStrBefore[3,1]{b1b2b3}{b}{2}{true}{false}\par \IfSubStrBefore[2,2]{baobab}{a}{b}{true}{false}\par \IfSubStrBefore[2,3]{baobab}{a}{b}{true}{false}| \subsubsection{\ttfamily\textbackslash IfSubStrBehind} \verb|\IfSubStrBehind|\etoile\arguCC{number1}{number2}\ARGU{string}\ARGU{stringA}\ARGU{stringB}\ARGU{true}\ARGU{false} \smallskip The values of the optional arguments \argu{number1} and \argu{number2} are 1 by default.\par\smallskip In \argu{string}, tests if the \argu{number1}\th occurrence of \argu{stringA} is on the right of the \argu{number2}\th occurrence of \argu{stringB}. Runs \argu{true} if so, and \argu{false} otherwise.\medskip \begin{Conditions} \item If one of the occurrences is not found, it runs \argu{false}; \item If one of the arguments \argu{string}, \argu{stringA} or \argu{stringB} is empty, runs \argu{false}; \item If one of the optional arguments is negative or zero, runs \argu{false}. \end{Conditions} \showcode|\IfSubStrBehind{xstring}{ri}{xs}{true}{false}\par \IfSubStrBehind{xstring}{s}{i}{true}{false}\par \IfSubStrBehind{LaTeX}{TeX}{LaT}{true}{false}\par \IfSubStrBehind{a bc def }{ d}{a}{true}{false}\par \IfSubStrBehind{a bc def }{cd}{a b}{true}{false}\par \IfSubStrBehind[2,1]{b1b2b3}{b}{2}{true}{false}\par \IfSubStrBehind[3,1]{b1b2b3}{b}{2}{true}{false}\par \IfSubStrBehind[2,2]{baobab}{b}{a}{true}{false}\par \IfSubStrBehind[2,3]{baobab}{b}{a}{true}{false}| \subsubsection{\ttfamily\textbackslash IfBeginWith} \verb|\IfBeginWith|\etoile\ARGU{string}\ARGU{stringA}\ARGU{true}\ARGU{false} \smallskip Tests if \argu{string} begins with \argu{stringA}, and runs \argu{true} if so, and \argu{false} otherwise.\medskip \begin{Conditions} \item If \argu{string} or \argu{stringA} is empty, runs \argu{false}. \end{Conditions} \showcode|\IfBeginWith{xstring}{xst}{true}{false}\par \IfBeginWith{LaTeX}{a}{true}{false}\par \IfBeginWith{a bc def }{a b}{true}{false}\par \IfBeginWith{a bc def }{ab}{true}{false}| \subsubsection{\ttfamily\textbackslash IfEndWith} \verb|\IfEndWith|\etoile\ARGU{string}\ARGU{stringA}\ARGU{Behind}\ARGU{false} \smallskip Tests if \argu{string} ends with \argu{stringA}, and runs \argu{true} if so, and \argu{false} otherwise.\medskip \begin{Conditions} \item If \argu{string} or \argu{stringA} is empty, runs \argu{false}. \end{Conditions} \showcode|\IfEndWith{xstring}{ring}{true}{false}\par \IfEndWith{LaTeX}{a}{true}{false}\par \IfEndWith{a bc def }{ef }{true}{false}\par \IfEndWith{a bc def }{ef}{true}{false}| \subsubsection{\ttfamily\textbackslash IfInteger} \verb|\IfInteger|\ARGU{number}\ARGU{true}\ARGU{false} \smallskip Tests if \argu{number} is an integer (i.e whose decimal part is empty or 0), and runs \argu{true} if so, and \argu{false} otherwise.\smallskip If test is false because unexpected characters, the control sequence \verb|\afterinteger| contains the illegal part of \argu{number}.\medskip \showcode|\IfInteger{13}{true}{false}\par \IfInteger{-219}{true}{false}\par \IfInteger{+9}{true}{false}\par \IfInteger{3.14}{true}{false}\par \IfInteger{8.0}{true}{false}\par \IfInteger{0}{true}{false}\par \IfInteger{49a}{true}{false}\par \IfInteger{+}{true}{false}\par \IfInteger{-}{true}{false}\par \IfInteger{0000}{true}{false}| \subsubsection{\ttfamily\textbackslash IfDecimal}\label{ifdecimal} \verb|\IfDecimal|\ARGU{number}\ARGU{true}\ARGU{false} \smallskip Tests if \argu{number} is a decimal, and runs \argu{true} if so, and \argu{false} otherwise.\smallskip Macros \verb|\integerpart| and \verb|\decimalpart| contain the integer part and decimal part of \argu{number}. If test is false because unexpected characters, the control sequence \verb|\afterdecimal| contain the illegal part of \argu{number}, whereas if test is false because decimal part is empty after decimal separator, it contain "X".\medskip \begin{Conditions} \item Decimal separator can be a dot or a comma; \item If what is on the right of decimal separator (if it exists) is empty, the test is false; \item If what is on the left of decimal separator (if it exists) is empty, the integer part is assumed to be 0; \end{Conditions} \showcode|\IfDecimal{3.14}{true}{false}\par \IfDecimal{3,14}{true}{false}\par \IfDecimal{-0.5}{true}{false}\par \IfDecimal{.7}{true}{false}\par \IfDecimal{,9}{true}{false}\par \IfDecimal{1..2}{true}{false}\par \IfDecimal{+6}{true}{false}\par \IfDecimal{-15}{true}{false}\par \IfDecimal{1.}{true}{false}\par \IfDecimal{2,}{true}{false}\par \IfDecimal{.}{true}{false}\par \IfDecimal{,}{true}{false}\par \IfDecimal{+}{true}{false}\par \IfDecimal{-}{true}{false}| \subsubsection{\ttfamily\textbackslash IfStrEq} \verb|\IfStrEq|\etoile\ARGU{stringA}\ARGU{stringB}\ARGU{true}\ARGU{false} \smallskip Tests if the strings \argu{stringA} and \argu{stringB} are equal, i.e. if they contain successively the same \USs in the same order. Runs \argu{true} if so, and \argu{false} otherwise.\smallskip \showcode|\IfStrEq{a1b2c3}{a1b2c3}{true}{false}\par \IfStrEq{abcdef}{abcd}{true}{false}\par \IfStrEq{abc}{abcdef}{true}{false}\par \IfStrEq{3,14}{3,14}{true}{false}\par \IfStrEq{12.34}{12.340}{true}{false}\par \IfStrEq{abc}{}{true}{false}\par \IfStrEq{}{abc}{true}{false}\par \IfStrEq{}{}{true}{false}| \subsubsection{\ttfamily\textbackslash IfEq} \verb|\IfEq|\ARGU{stringA}\ARGU{stringB}\ARGU{true}\ARGU{false} \smallskip Tests if the strings \argu{stringA} and \argu{stringB} are equal, \emph{except} if both \argu{stringA} and \argu{stringB} contain numbers in which case the macro tests if these numbers are equal. Runs \argu{true} if so, and \argu{false} otherwise.\smallskip \begin{Conditions} \item The definition of \emph{number} is given with the macro \verb|\IfDecimal| (see page~\pageref{ifdecimal}), and thus : \item \guill{+} signs are optional; \item Decimal separator can be a dot or a comma. \end{Conditions} \showcode|\IfEq{a1b2c3}{a1b2c3}{true}{false}\par \IfEq{abcdef}{ab}{true}{false}\par \IfEq{ab}{abcdef}{true}{false}\par \IfEq{12.34}{12,34}{true}{false}\par \IfEq{+12.34}{12.340}{true}{false}\par \IfEq{10}{+10}{true}{false}\par \IfEq{-10}{10}{true}{false}\par \IfEq{+0,5}{,5}{true}{false}\par \IfEq{1.001}{1.01}{true}{false}\par \IfEq{3*4+2}{14}{true}{false}\par \IfEq{\number\numexpr3*4+2}{14}{true}{false}\par \IfEq{0}{-0.0}{true}{false}\par \IfEq{}{}{true}{false}| \subsubsection{\ttfamily\textbackslash IfStrEqCase} \begin{minipage}{\textwidth} \verb|\IfStrEqCase|\etoile\ARGU{string}\verb|{%|\par \qquad\qquad\ARGU{string1}\ARGU{code1}\verb|%|\par \qquad\qquad\ARGU{string2}\ARGU{code2}\verb|%|\par \qquad\qquad\verb|etc...|\par \qquad\qquad\ARGU{stringN}\ARGU{codeN}\verb|}|\arguC{other cases code} \end{minipage} \smallskip Tests successively if \argu{string} is equal to \argu{string1}, \argu{string2}, etc. Comparison is made with \verb|\IfStrEq| (see above). If the test number $i$ is positive (the \argu{string} matches \argu{string\;$i$}), the macro runs \argu{code\;$i$} and ends. If all tests fail, the macro runs the optional \argu{other cases code}, if present.\smallskip \showcode-\IfStrEqCase{b}{{a}{AA}{b}{BB}{c}{CC}}\par |\IfStrEqCase{abc}{{a}{AA}{b}{BB}{c}{CC}}|\par \IfStrEqCase{c}{{a}{AA}{b}{BB}{c}{CC}}[other]\par \IfStrEqCase{d}{{a}{AA}{b}{BB}{c}{CC}}[other]\par \IfStrEqCase{+3}{{1}{un}{2}{deux}{3}{trois}}[other]\par \IfStrEqCase{0.5}{{0}{zero}{.5}{demi}{1}{un}}[other]- \subsubsection{\ttfamily\textbackslash IfEqCase} \begin{minipage}{\textwidth} \verb|\IfEqCase|\etoile\ARGU{string}\verb|{%|\par \qquad\qquad\ARGU{string1}\ARGU{code1}\verb|%|\par \qquad\qquad\ARGU{string2}\ARGU{code2}\verb|%|\par \qquad\qquad\verb|etc...|\par \qquad\qquad\ARGU{stringN}\ARGU{codeN}\verb|}|\arguC{other cases code} \end{minipage} \smallskip Tests successively if \argu{string} is equal to \argu{string1}, \argu{string2}, etc. Comparison is made with \verb|\IEq| (see above). If the test number $i$ is positive (the \argu{string} matches \argu{string\;$i$}), the macro runs \argu{code\;$i$} and ends. If all tests fail, the macro runs the optional \argu{other cases code}, if present.\smallskip \showcode-\IfEqCase{b}{{a}{AA}{b}{BB}{c}{CC}}\par |\IfEqCase{abc}{{a}{AA}{b}{BB}{c}{CC}}|\par \IfEqCase{c}{{a}{AA}{b}{BB}{c}{CC}}[other]\par \IfEqCase{d}{{a}{AA}{b}{BB}{c}{CC}}[other]\par \IfEqCase{+3}{{1}{un}{2}{deux}{3}{trois}}[other]\par \IfEqCase{0.5}{{0}{zero}{.5}{demi}{1}{un}}[other]- \subsection{Extraction of substrings} \subsubsection{\ttfamily\textbackslash StrBefore} \verb|\StrBefore|\etoile\arguC{number}\ARGU{string}\ARGU{stringA}\arguC{name} \smallskip The value of the optional argument \argu{number} is 1 by default.\par\smallskip In \argu{string}, returns what is leftwards the \argu{number}\th occurrence of \argu{stringA}.\medskip \begin{Conditions} \item If \argu{string} or \argu{stringA} is empty, an empty string is returned; \item If \argu{number}${}<1$ then the macro behaves as if \argu{number}${}=1$; \item If the occurrence is not found, an empty string is returned. \end{Conditions} \showcode-\StrBefore{xstring}{tri}\par \StrBefore{LaTeX}{e}\par |\StrBefore{LaTeX}{p}|\par |\StrBefore{LaTeX}{L}|\par |\StrBefore{a bc def }{def}|\par |\StrBefore{a bc def }{cd}|\par \StrBefore[1]{1b2b3}{b}\par \StrBefore[2]{1b2b3}{b}- \subsubsection{\ttfamily\textbackslash StrBehind} \verb|\StrBehind|\etoile\arguC{number}\ARGU{string}\ARGU{stringA}\arguC{name} \smallskip The value of the optional argument \argu{number} is 1 by default.\par\smallskip In \argu{string}, returns what is rightwards the \argu{number}\th occurrence of \argu{stringA}.\medskip \begin{Conditions} \item If \argu{string} or \argu{stringA} is empty, an empty string is returned; \item If \argu{number}${}<1$ then the macro behaves as if \argu{number}${}=1$; \item If the occurrence is not found, an empty string is returned. \end{Conditions} \showcode-\StrBehind{xstring}{tri}\par \StrBehind{LaTeX}{e}\par |\StrBehind{LaTeX}{p}|\par |\StrBehind{LaTeX}{X}|\par |\StrBehind{a bc def }{bc}|\par |\StrBehind{a bc def }{cd}|\par \StrBehind[1]{1b2b3}{b}\par \StrBehind[2]{1b2b3}{b}\par |\StrBehind[3]{1b2b3}{b}- \subsubsection{\ttfamily\textbackslash StrCut} Here is the syntax of this macro:\par\nobreak\smallskip \verb|\StrCut|\etoile\arguC{nunber}\ARGU{string}\ARGU{stringA}\ARGU{macroA}\ARGU{macroB} \smallskip The optional argument \argu{number} is 1 by default.\par\nobreak\smallskip The \argu{string} is cut in two parts at the occurrence \arguC{number} of \ARGU{stringA}. The left part is stored in the control sequence \argu{macroA} and the right part in \argu{macroB}. Since this macro returns \emph{two strings}, it does \emph{not} display anything. Consequently, it does not provide the optional argument in last position.\medskip \begin{Conditions} \item If \argu{string} or \argu{stringA} is empty, \argu{macroA} and \argu{macroB} are empty; \item If \argu{number}${}<1$, the macro behaves as if \argu{number}${}=1$; \item If the occurrence is not found, \argu{macroA} receives the whole \argu{string} while \argu{macroB} is empty. \end{Conditions} \showcode+\def\seprouge{{\color{red}|}} \StrCut{abracadabra}{a}\csA\csB |\csA\seprouge\csB|\par \StrCut[2]{abracadabra}{a}\csA\csB |\csA\seprouge\csB|\par \StrCut[3]{abracadabra}{a}\csA\csB |\csA\seprouge\csB|\par \StrCut[4]{abracadabra}{a}\csA\csB |\csA\seprouge\csB|\par \StrCut[5]{abracadabra}{a}\csA\csB |\csA\seprouge\csB|\par \StrCut[6]{abracadabra}{a}\csA\csB |\csA\seprouge\csB|\par \StrCut[-4]{abracadabra}{a}\csA\csB |\csA\seprouge\csB|\par \StrCut{abracadabra}{brac}\csA\csB |\csA\seprouge\csB|\par \StrCut{abracadabra}{foo}\csA\csB |\csA\seprouge\csB|\par \StrCut{abracadabra}{}\csA\csB |\csA\seprouge\csB|+ \subsubsection{\ttfamily\textbackslash StrBetween} \verb|\StrBetween|\etoile\arguCC{number1}{number2}\ARGU{string}\ARGU{stringA}\ARGU{stringB}\arguC{name} \smallskip The values of the optional arguments \argu{number1} and \argu{number2} are 1 by default.\par\smallskip In \argu{string}, returns the substring between\footnote{In a strict sense, i.e. \emph{without} the strings \argu{stringA} and \argu{stringB}} the \argu{number1}\th occurrence of \argu{stringA} and \argu{number2}\th occurrence of \argu{stringB}.\medskip \begin{Conditions} \item If the occurrences are not in this order ---~\argu{stringA} \emph{followed by} \argu{stringB}~--- in \argu{string}, an empty string is returned; \item If one of the 2 occurrences doesn't exist in \argu{string}, an empty string is returned; \item If one of the optional arguments \argu{number1} ou \argu{number2} is negative or zero, an empty string is returned. \end{Conditions} \showcode+\StrBetween{xstring}{xs}{ng}\par |\StrBetween{xstring}{i}{n}|\par |\StrBetween{xstring}{a}{tring}|\par |\StrBetween{a bc def }{a}{d}|\par |\StrBetween{a bc def }{a }{f}|\par \StrBetween{a1b1a2b2a3b3}{a}{b}\par \StrBetween[2,3]{a1b1a2b2a3b3}{a}{b}\par \StrBetween[1,3]{a1b1a2b2a3b3}{a}{b}\par |\StrBetween[3,1]{a1b1a2b2a3b3}{a}{b}|\par \StrBetween[3,2]{abracadabra}{a}{bra}+ \subsubsection{\ttfamily\textbackslash StrSubstitute} \verb|\StrSubstitute|\arguC{number}\ARGU{string}\ARGU{stringA}\ARGU{stringB}\arguC{name} \smallskip The value of the optional argument \argu{number} is 0 by default.\par\smallskip In \argu{string}, substitute the \argu{number} first occurrences of \argu{stringA} for \argu{stringB}, except if \argu{number}${}=0$ in which case \emph{all} the occurrences are substituted. \begin{Conditions} \item If \argu{string} is empty, an empty string is returned; \item If \argu{stringA} is empty or doesn't exist in \argu{string}, the macro is ineffective; \item If \argu{number} is greater than the number of occurrences of \argu{stringA}, then all the occurrences are substituted; \item If \argu{number}${}<0$ the macro behaves as if \argu{number}${}=0$; \item If \argu{stringB} is empty, the occurrences of \argu{stringA}, if they exist, are deleted. \end{Conditions} \showcode|\StrSubstitute{xstring}{i}{a}\par \StrSubstitute{abracadabra}{a}{o}\par \StrSubstitute{abracadabra}{br}{TeX}\par \StrSubstitute{LaTeX}{m}{n}\par \StrSubstitute{a bc def }{ }{M}\par \StrSubstitute{a bc def }{ab}{AB}\par \StrSubstitute[1]{a1a2a3}{a}{B}\par \StrSubstitute[2]{a1a2a3}{a}{B}\par \StrSubstitute[3]{a1a2a3}{a}{B}\par \StrSubstitute[4]{a1a2a3}{a}{B}| \subsubsection{\ttfamily\textbackslash StrDel} \verb|\StrDel|\etoile\arguC{number}\ARGU{string}\ARGU{stringA}\arguC{name} \smallskip The value of the optional argument \argu{number} is 0 by default.\par\smallskip Delete the \argu{number} first occurrences of \argu{stringA} in \argu{string}, except if \argu{number}${}=0$ in which case \emph{all} the occurrences are deleted.\medskip \begin{Conditions} \item If \argu{string} is empty, an empty string is returned; \item If \argu{stringA} is empty or doesn't exist in \argu{string}, the macro is ineffective; \item If \argu{number} greater then the number of occurrences of \argu{stringA}, then all the occurrences are deleted; \item If \argu{number}${}<0$ the macro behaves as if \argu{number}${}=0$; \end{Conditions} \showcode+\StrDel{abracadabra}{a}\par \StrDel[1]{abracadabra}{a}\par \StrDel[4]{abracadabra}{a}\par \StrDel[9]{abracadabra}{a}\par \StrDel{a bc def }{ }\par |\StrDel{a bc def }{def}|+ \subsubsection{\ttfamily\textbackslash StrGobbleLeft} \verb|\StrGobbleLeft|\ARGU{string}\ARGU{number}\arguC{name} \smallskip In \argu{string}, delete the \argu{number} first characters on the left.\medskip \begin{Conditions} \item If \argu{string} is empty, an empty string is returned; \item If \argu{number}${}\leqslant0$, no character is deleted; \item If \argu{number}${}\geqslant{}$\argu{lengthString}, all the characters are deleted. \end{Conditions} \showcode+\StrGobbleLeft{xstring}{2}\par |\StrGobbleLeft{xstring}{9}|\par \StrGobbleLeft{LaTeX}{4}\par \StrGobbleLeft{LaTeX}{-2}\par |\StrGobbleLeft{a bc def }{4}|+ \subsubsection{\ttfamily\textbackslash StrLeft} \verb|\StrLeft|\ARGU{string}\ARGU{number}\arguC{name} \smallskip In \argu{string}, returns the \argu{number} first characters on the left.\medskip \begin{Conditions} \item If \argu{string} is empty, an empty string is returned; \item If \argu{number}${}\leqslant0$, no character is returned; \item If \argu{number}${}\geqslant{}$\argu{lengthString}, all the characters are returned. \end{Conditions} \showcode+\StrLeft{xstring}{2}\par \StrLeft{xstring}{9}\par \StrLeft{LaTeX}{4}\par |\StrLeft{LaTeX}{-2}|\par |\StrLeft{a bc def }{5}|+ \subsubsection{\ttfamily\textbackslash StrGobbleRight} \verb|\StrGobbleRight|\ARGU{string}\ARGU{number}\arguC{name} \smallskip In \argu{string}, delete the \argu{number} last characters on the right.\medskip \showcode+\StrGobbleRight{xstring}{2}\par |\StrGobbleRight{xstring}{9}|\par \StrGobbleRight{LaTeX}{4}\par |\StrGobbleRight{LaTeX}{-2}|\par |\StrGobbleRight{a bc def }{4}|+ \subsubsection{\ttfamily\textbackslash StrRight} \verb|\StrRight|\ARGU{string}\ARGU{number}\arguC{name} \smallskip In \argu{string}, returns the \argu{number} last characters on the right.\medskip \showcode+\StrRight{xstring}{2}\par \StrRight{xstring}{9}\par \StrRight{LaTeX}{4}\par |\StrRight{LaTeX}{-2}|\par \StrRight{a bc def }{5}+ \subsubsection{\ttfamily\textbackslash StrChar} \verb|\StrChar|\etoile\ARGU{string}\ARGU{number}\arguC{name} \smallskip Returns the \US at the position \argu{number} in \argu{string}.\medskip \begin{Conditions} \item If \argu{string} is empty, no caracter is returned; \item If \argu{number}${}\leqslant0$ or if \argu{number}${}>{}$\argu{lengthString}, no character is returned. \end{Conditions} \showcode+\StrChar{xstring}{4}\par |\StrChar{xstring}{9}|\par |\StrChar{xstring}{-5}|\par \StrChar{a bc def }{6}+ \subsubsection{\ttfamily\textbackslash StrMid} \verb|\StrMid|\ARGU{string}\ARGU{numberA}\ARGU{numberB}\arguC{name} \smallskip In \argu{string}, returns the substring between\footnote{In the broad sense, i.e. that the strings characters of the \guill{border} are returned.} the positions \argu{numberA} and \argu{numberB}.\medskip \begin{Conditions} \item If \argu{string} is empty, an empty string is returned; \item If \argu{numberA}${}>{}$\argu{numberB}, an empty string is returned; \item If \argu{numberA}${}<1$ and \argu{numberB}${}<1$ an empty string is returned; \item If \argu{numberA}${}>{}$\argu{lengthString} et \argu{numberB}${}>{}$\argu{lengthString}, an empty string is returned; \item If \argu{numberA}${}<1$, the macro behaves as if \argu{numberA}${}=1$; \item If \argu{numberB}${}>{}$\argu{lengthString}, the macro behaves as if \argu{numberB}${}={}$\argu{lengthString}. \end{Conditions} \showcode+\StrMid{xstring}{2}{5}\par \StrMid{xstring}{-4}{2}\par |\StrMid{xstring}{5}{1}|\par \StrMid{xstring}{6}{15}\par \StrMid{xstring}{3}{3}\par |\StrMid{a bc def }{2}{7}|+ \subsection{Macros returning a number} \subsubsection{\ttfamily\textbackslash StrLen} \verb|\StrLen|\ARGU{string}\arguC{name} \smallskip Return the length of \argu{string}. \showcode+\StrLen{xstring}\par \StrLen{A}\par \StrLen{a bc def }+ \subsubsection{\ttfamily\textbackslash StrCount} \verb|\StrCount|\ARGU{string}\ARGU{stringA}\arguC{name} \smallskip Counts how many times \argu{stringA} is contained in \argu{string}.\par\medskip \begin{Conditions} \item If one at least of the arguments \argu{string} or \argu{stringA} is empty, the macro return 0. \end{Conditions} \showcode+\StrCount{abracadabra}{a}\par \StrCount{abracadabra}{bra}\par \StrCount{abracadabra}{tic}\par \StrCount{aaaaaa}{aa}+ \subsubsection{\ttfamily\textbackslash StrPosition} \verb|\StrPosition|\arguC{number}\ARGU{string}\ARGU{stringA}\arguC{name} \smallskip The value of the optional argument \argu{number} is 1 by default.\par\smallskip In \argu{string}, returns the position of the \argu{number}\th occurrence of \argu{stringA}.\medskip \begin{Conditions} \item If \argu{number} is greater than the number of occurrences of \argu{stringA}, then the macro returns 0; \item If \argu{string} doesn't contain \argu{stringA}, then the macro returns 0. \end{Conditions} \medskip \showcode+\StrPosition{xstring}{ring}\par \StrPosition[4]{abracadabra}{a}\par \StrPosition[2]{abracadabra}{bra}\par \StrPosition[9]{abracadabra}{a}\par \StrPosition{abracadabra}{z}\par \StrPosition{a bc def }{d}\par \StrPosition[3]{aaaaaa}{aa}+ \subsubsection{\ttfamily\textbackslash StrCompare} \verb|\StrCompare|\etoile\ARGU{stringA}\ARGU{stringB}\arguC{name} \smallskip This macro has 2 tolerances: the "normal" tolerance, used by default, and the "strict" tolerance. \begin{itemize} \item The normal tolerance is activated with \verb|\comparenormal|.\par The macro compares characters from left to right in \argu{stringA} and \argu{stringB} until a difference appears or the end of the shortest string is reached. The position of the first difference is returned and if no difference is found, the macro return 0. \item The strict tolerance is activated with \verb|\comparestrict|.\par The macro compares the 2 strings. If they are equal, it returns 0. If not, the position of the first difference is returned. \end{itemize} \medskip It is possible to save the comparison mode with \verb|\savecomparemode|, then modify this comparison mode and come back to the situation when it was saved with \verb|\restorecomparemode|.\medskip Examples with the normal tolerance:\par\nobreak \showcode+ \comparenormal \StrCompare{abcd}{abcd}\par \StrCompare{abcd}{abc}\par \StrCompare{abc}{abcd}\par \StrCompare{a b c}{abc}\par \StrCompare{aaa}{baaa}\par \StrCompare{abc}{xyz}\par \StrCompare{123456}{123457}\par \StrCompare{abc}{}+ Examples with the strict tolerance:\par\nobreak \showcode+\comparestrict \StrCompare{abcd}{abcd}\par \StrCompare{abcd}{abc}\par \StrCompare{abc}{abcd}\par \StrCompare{a b c}{abc}\par \StrCompare{aaa}{baaa}\par \StrCompare{abc}{xyz}\par \StrCompare{123456}{123457}\par \StrCompare{abc}{}+ \section{Operating modes} \subsection{Expansion of arguments} \label{devarg} \subsubsection{The commands {\ttfamily \textbackslash fullexpandarg}, {\ttfamily \textbackslash expandarg} and {\ttfamily \textbackslash noexpandarg}} The command \verb|\fullexpandarg| is called by default, so all the arguments are fully expanded (an \verb|\edef| is used) before the the macro works on them. In most of the cases, this expansion mode avoids chains of \verb|\expandafter| and allows lighter code. Of course, the expansion of argument can be canceled to find back the usual behaviour of \TeX{} with the commands \verb|\noexpandarg| or \verb|\normalexpandarg|.\medskip Another expansion mode can be called with \verb|\expandarg|. In this case, the \textbf{first token} of each argument is expanded \emph{one time} while all other tokens are left unchanged (if you want the expansion of all tokens one time, you should call the macro \verb|\StrExpand|, see page~\pageref{scancs}).\medskip The commands \verb|\fullexpandarg|, \verb|\noexpandarg|, \verb|\normalexpandarg| and \verb|\expandarg| can be called at any moment in the code; they behave as "switches" and they can be locally used in a group.\medskip It is possible to save the expansion mode with \verb|\saveexpandmode|, then modify this expansion mode and come back to the situation when it was saved with \verb|\restoreexpandmode|.\medskip In the following list, for every macro of the previous chapter, the arguments colored in \textcolor{violet}{purple} will possibly be expanded, according to the expansion mode:\smallskip \parindent3em \begin{itemize} \item\verb|\IfSubStr|\etoile{\colorise\arguC{number}\ARGU{string}\ARGU{stringA}}\ARGU{true}\ARGU{false} \item\verb|\IfSubStrBefore|\etoile{\colorise\arguCC{number1}{number2}\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} \item\verb|\IfSubStrBehind|\etoile{\colorise\arguCC{number1}{number2}\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} \item\verb|\IfBeginWith|\etoile{\colorise\ARGU{string}\ARGU{stringA}}\ARGU{true}\ARGU{false} \item\verb|\IfEndWith|\etoile{\colorise\ARGU{string}\ARGU{stringA}}\ARGU{true}\ARGU{false} \item\verb|\IfInteger|{\colorise\ARGU{number}}\ARGU{true}\ARGU{false} \item\verb|\IfDecimal|{\colorise\ARGU{number}}\ARGU{true}\ARGU{false} \item\verb|\IfStrEq|\etoile{\colorise\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} \item\verb|\IfEq|\etoile{\colorise\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} \item\verb|\IfStrEqCase|\etoile{\colorise\ARGU{string}\texttt{\color{black}\{}\colorise\ARGU{string1}}\ARGU{code1}\par \setbox0=\hbox{{\ttfamily\string\IfStrEqCase}\etoile\ARGU{string}{\ttfamily\{}} \hskip\wd0{\colorise\ARGU{string2}}\ARGU{code2}\par \hskip\wd0\ \ldots\par \hskip\wd0{\colorise\ARGU{string $n$}}\ARGU{code $n$}\verb|}|\arguC{other cases code} \item\verb|\IfEqCase|\etoile{\colorise\ARGU{string}\texttt{\color{black}\{}\colorise\ARGU{string1}}\ARGU{code1}\par \setbox0=\hbox{{\ttfamily\string\IfEqCase}\etoile\ARGU{string}{\ttfamily\{}} \hskip\wd0{\colorise\ARGU{string2}}\ARGU{code2}\par \hskip\wd0\ \ldots\par \hskip\wd0{\colorise\ARGU{string $n$}}\ARGU{code $n$}\verb|}|\arguC{other cases code} \item\verb|\StrBefore|\etoile{\colorise\arguC{number}\ARGU{string}\ARGU{stringA}}\arguC{name} \item\verb|\StrBehind|\etoile{\colorise\arguC{number}\ARGU{string}\ARGU{stringA}}\arguC{name} \item\verb|\StrBetween|\etoile{\colorise\arguCC{number1}{number2}\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\arguC{name} \item\verb|\StrSubstitute|{\colorise\arguC{number}\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\arguC{name} \item\verb|\StrDel|{\colorise\arguC{number}\ARGU{string}\ARGU{stringA}}\arguC{name} \item\verb|\StrSplit|{\colorise\ARGU{string}\ARGU{number}}\ARGU{stringA}\ARGU{stringB}\qquad{\small(see macro \verb|StrSplit| page~\pageref{StrSplit})} \item\verb|\StrGobbleLeft|{\colorise\ARGU{string}\ARGU{number}}\arguC{name} \item\verb|\StrLeft|{\colorise\ARGU{string}\ARGU{number}}\arguC{name} \item\verb|\StrGobbleRight|{\colorise\ARGU{string}\ARGU{number}}\arguC{name} \item\verb|\StrRight|{\colorise\ARGU{string}\ARGU{number}}\arguC{name} \item\verb|\StrChar|{\colorise\ARGU{string}\ARGU{number}}\arguC{name} \item\verb|\StrMid|{\colorise\ARGU{string}\ARGU{number1}\ARGU{number2}}\arguC{name} \item\verb|\StrLen|{\colorise\ARGU{string}}\arguC{name} \item\verb|\StrCount|{\colorise\ARGU{string}\ARGU{stringA}}\arguC{name} \item\verb|\StrPosition|{\colorise\arguC{number}\ARGU{string}\ARGU{stringA}}\arguC{name} \item\verb|\StrCompare|{\colorise\ARGU{stringA}\ARGU{stringB}}\arguC{name} \end{itemize} \parindent0pt \subsubsection{Chars and tokens allowed in arguments} First of all, whatever be the current expansion mode, \textbf{tokens with catcode 6 and 14 (usually {\ttfamily\#} and {\ttfamily\%}) are forbidden in all the arguments}\footnote{Maybe, the token {\ttfamily\#} will be allowed in a future version.}.\bigskip When full expansion mode is activated with \verb|\fullexpandarg|, arguments are expanded with an \verb|\edef| before they are read by the macro. Consequently, are allowed in arguments: \parindent3em \begin{itemize} \item letters (uppercase or lowercase, accented\footnote{For a reliable operation with accented letters, the \texttt{\string\fontenc} package with option \texttt{[T1]} and \texttt{\string\inputenc} with appropriated option must be loaded} or not), figures, spaces, and any other character with a catcode of 10, 11 ou 12 (punctuation signs, calculation signs, parenthesis, square bracket, etc).; \item tokens with catcode 1 to 4, usually : \verb|{|\quad\verb|}|\footnote{Warning : braces \textbf{must} be balanced in arguments !}\quad\verb|$|\quad\verb|&| \item tokens with catcode 7 and 8, usually : \verb|^|\quad\verb|_| \item any purely expandable control sequence\footnote{i.e. this control sequence can be {\ttfamily\string\edef}ed.} or tokens with catcode 13 (active chars) whose expansion is allowed chars. \end{itemize}\parindent0pt \parindent0pt\smallskip When arguments are not expanded with the use of \verb|\noexpandarg|, other tokens can be put in a string whatever be the code they make: any control sequence, even undefined, any token catcode 13. Moreover, test tokens are allowed like \verb|\if| or \verb|\ifx|, even without their \verb|\fi|. On the same way, a \verb|\csname| without its \verb|\endcsname| is allowed. In this example, the argument contains a \verb|\ifx| \emph{without} the \verb|\fi|: the \verb|\StrBetween| command extracts and dislpays what is between \verb|\ifx| and \verb|\else|:\par\medskip\nobreak \showcode+\noexpandarg \StrBetween{\ifx ab false \else true}{\ifx}{\else}+ \medskip When \verb|\expandarg| is used, the first token needs precaution since it is expanded one time: it must be defined. The other tokens are left unchanged like with \verb|\noexpandarg|. \subsection{Expansion of macros, optional argument} \label{argumentoptionnel} The macros of this package are not purely expandable, i.e. they cannot be put in the argument of an \verb|\edef|. Nestling macros is not possible neither.\medskip For this reason, all the macros returning a result (i.e. all excepted the tests) have an optional argument in last position. The syntax is \arguC{name}, where \argu{name} is the name of the control sequence that will receive the result of the macro: the assignment is made with an \verb|\edef| which make the result of the macro \argu{name} purely expandable. Of course, if an optional argument is present, the macro does not display anything.\medskip Thus, this structure not allowed, supposed to assign to \verb|\Result| the 4 chars on the left of \verb|xstring|:\par\nobreak \hspace{0.2\linewidth}\verb|\edef\Result{\StrLeft{xstring}{4}}|\par\nobreak \qquad is equivalent to :\par\nobreak \hspace{0.2\linewidth}\verb|\StrLeft{xstring}{4}[\Result]|\medskip And this nested structure, not allowed neither, supposed to remove the first and last char of \verb|xstring|:\par\nobreak \hspace{0.2\linewidth}\verb|\StrGobbleLeft{\StrGobbleRight{xstring}{1}}{1}|\par\nobreak \qquad should be written like this:\par\nobreak \hspace{0.2\linewidth}\verb|\StrGobbleRight{xstring}{1}[\mystring]|\par\nobreak \hspace{0.2\linewidth}\verb|\StrGobbleleft{\mystring}{1}|\par \subsection{How does \Xstring read the arguments?} \label{developpementarguments} \subsubsection{Syntax unit by \US} The macros of \Xstring read their arguments \US par \US. In the \TeX{} code, a \US\footnote{For advanced users used to \LaTeX{} programming, a \US is what is gobbled by the macro \texttt{\string\@gobble} whose code is: {\ttfamily\string\def\string\@gobble\string#1\string{\string}}} is either:\parindent3em \begin{itemize} \item a control sequence; \item a group, i.e. what is between 2 balanced braces (usually tokens catcode 1 and 2); \item a char. \end{itemize} \medskip\parindent0pt Let's see what is a \US with an example. Let's take this argument : "\verb|ab\textbf{xyz}cd|" It has 6 \USs: "\verb|a|", "\verb|b|", "\verb|\textbf|", "\verb|{xyz}|", "\verb|c|" and "\verb|d|".\medskip What will happen if, while \verb|\noexpandarg| is active, we ask \Xstring to find the length of this argument and find its 4\th "char"\medskip \showcode+\noexpandarg \StrLen{ab\textbf{xyz}cd}\par \StrChar{ab\textbf{xyz}cd}{4}[\mychar] \meaning\mychar+ \fullexpandarg \medskip It is necessary to use \verb|\meaning| to see the real expansion of \verb|\mychar|, and not simply call \verb|\mychar| to display it, which make loose informations (braces here). We do not obtain a "char" but a \US, as expected. \subsubsection{Exploration of groups} By default, the command \verb|\noexploregroups| is called, so in the argument containing the string of tokens, \Xstring does not look into groups, and simply consider them as a \US. For specific uses, it can be necessary to look into groups: \verb|\exploregroups| changes the exploration mode and forces the macros to look inside groups.\medskip What does this exploration mode in the previous example? \Xstring does not count the group as a single \US but looks inside it and counts the \US found inside (\verb|x|, \verb|y| and \verb|z|), and so on if there were several nested groups:\smallskip \nobreak\smallskip \showcode+\noexpandarg \exploregroups \StrLen{ab\textbf{xyz}cd}\par \StrChar{ab\textbf{xyz}cd}{4}[\mychar] \meaning\mychar+ \fullexpandarg\noexploregroups \medskip Exploring the groups can be usefull for counting a substring in a string (\verb|\StrCount|), for the position of a substring in a string (\verb|\StrPosition|) or for tests, but has a severe limitation with macros returning a string: when a string is cut inside a group, \textbf{the result does not take into account what is outside this group}. This exploration mode must be used knowingly this limitation when calling macros returning a string.\smallskip Let's see what this means with an example. We want to know what is on the left of the second appearance of \verb|\a| in the argument \verb|\a1{\b1\a2}\a3|. As groups are explored, this appearance is inside this group : \verb|{\b1\a2}|. The result will be \verb|\b1|. Let's check:\par\nobreak\smallskip \showcode+\noexpandarg \exploregroups \StrBefore[2]{\a1{\b1\a2}\a3}{\a}[\mycs] \meaning\mycs+ Exploring the groups\footnote{The file test of \Xstring has many examples underlining differences between exploration modes.} can change the behaviour of most of the macros of \Xstring, excepted these macros untouched by the exploration mode; their behaviour is the same in any case: \verb|\IfInteger|, \verb|\IfDecimal|, \verb|\IfStrEq|, \verb|\StrEq| et \verb|\StrCompare|. Moreover, 2 macros run in \verb|\noexploregroups| mode, whatever be the current mode: \verb|\StrBetween| et \verb|\StrMid|.\medskip It is possible to save the exploration mode with \verb|\saveexploremode|, then modify it and come back to the situation when it was saved with \verb|\restoreexploremode|. \subsection{Catcode and starred macros} \label{macrosetoilees} Macros of this package take the catcodes of tokens into account. To avoid unexpected behaviour (particulary with tests), you should keep in mind that tokens \emph{and their catcodes} are examined.\medskip For instance, these two arguments:\par\medskip \hfil\verb|{\string a\string b}|\qquad and\qquad\verb|{ab}|\hfil{}\par\smallskip do \emph{not} expand into equal strings for xstring! Because of the command \verb|\string|, the first expands into "\verb|ab|" with catcodes 12 while the second have characters with their natural catcodes 11. Catcodes do not match! It is necessary to be aware of this, particulary with \TeX{} commands like \verb|\string| whose expansions are a strings with chars catcodes 12 and 10 : \verb|\detokenize|, \verb|\meaning|, \verb|\jobname|, \verb|\fontname|, \verb|\romannumeral|, etc.\medskip Starred macros do not take catcodes into account. They simply convert some arguments into arguments with catcodes 10, 11 and 12, and call the non-starred macros with these modified arguments. The optional arguments are not modified and the catcodes are left unchanged.\medskip Here is an example:\par\nobreak\smallskip \showcode+\IfStrEq{\string a\string b}{ab}{true}{false}\par \IfStrEq*{\string a\string b}{ab}{true}{false}+ \smallskip The strings do not match because of catcode differences: the test is negative in the non-starred macro.\bigskip \textbf{Warning}: the use of a strarred macro has consequences! The arguments are \guill{detokenized}, thus, there is no more control sequences, groups, neither any special char: everything is converted into chars with "harmless" catcodes.\medskip For the macros returning a string, if the starred version is used, the result will be a string in which chars have catcodes 12 and 10 for space. For example, after a "\verb|\StrBefore*{a \b c d}{c}[\mytext]|", the control sequence \verb|\mytext| expands to "\verb|a|${}_{12}$\verb*| |${}_{10}$\verb|\|${}_{12}$\verb|b|${}_{12}$\verb*| |${}_{10}$".\medskip The macro with a starred version are listed below. For these macros, if starred version is used, the \textcolor{violet}{purple} arguments will be detokenized:\smallskip \parindent3em \begin{itemize} \item\verb|\IfSubStr|\etoile\arguC{number}{\colorise\ARGU{string}\ARGU{stringA}}\ARGU{true}\ARGU{false} \item\verb|\IfSubStrBefore|\etoile\arguCC{number1}{number2}{\colorise\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} \item\verb|\IfSubStrBehind|\etoile\arguCC{number1}{number2}{\colorise\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} \item\verb|\IfBeginWith|\etoile{\colorise\ARGU{string}\ARGU{stringA}}\ARGU{true}\ARGU{false} \item\verb|\IfEndWith|\etoile{\colorise\ARGU{string}\ARGU{stringA}}\ARGU{true}\ARGU{false} \item\verb|\IfStrEq|\etoile{\colorise\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} \item\verb|\IfEq|\etoile{\colorise\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} \item\verb|\IfStrEqCase|\etoile{\colorise\ARGU{string}\texttt{\color{black}\{}\colorise\ARGU{string1}}\ARGU{code1}\par \setbox0=\hbox{{\ttfamily\string\IfStrEqCase}\etoile\ARGU{string}{\ttfamily\{}} \hskip\wd0{\colorise\ARGU{string2}}\ARGU{code2}\par \hskip\wd0\ \ldots\par \hskip\wd0{\colorise\ARGU{string $n$}}\ARGU{code $n$}\verb|}|\arguC{other cases code} \item\verb|\IfEqCase|\etoile{\colorise\ARGU{string}\texttt{\color{black}\{}\colorise\ARGU{string1}}\ARGU{code1}\par \setbox0=\hbox{{\ttfamily\string\IfEqCase}\etoile\ARGU{string}{\ttfamily\{}} \hskip\wd0{\colorise\ARGU{string2}}\ARGU{code2}\par \hskip\wd0\ \ldots\par \hskip\wd0{\colorise\ARGU{string $n$}}\ARGU{code $n$}\verb|}|\arguC{other cases code} \item\verb|\StrBefore|\etoile\arguC{number}{\colorise\ARGU{string}\ARGU{stringA}}\arguC{name} \item\verb|\StrBehind|\etoile\arguC{number}{\colorise\ARGU{string}\ARGU{stringA}}\arguC{name} \item\verb|\StrBetween|\etoile\arguCC{number1}{number2}{\colorise\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\arguC{name} \item\verb|\StrCompare|\etoile{\colorise\ARGU{stringA}\ARGU{stringB}}\arguC{name} \end{itemize} \parindent0pt \section{Advanced macros for programming} Though \Xstring is able to read arguments containing \TeX{} or \LaTeX{} code, for some advanced programming needs, it can be insufficient. This chapter presents other macros able to get round some limitations. \subsection{Finding a group, macros {\ttfamily\textbackslash StrFindGroup} and {\ttfamily\textbackslash groupID}} When \verb=\exploregroups= mode is active, the macro \verb=\StrFindGroup= finds a group between braces with its identifier:\smallskip \verb|\StrFindGroup|\ARGU{argument}\ARGU{identifier}\arguC{name}\medskip When the group matching the identifier does not exist, an empty string is assigned to \argu{name}. If the group is found, this group \emph{with its braces} is assigned to \argu{name}.\smallskip This identifier characterizes the nestling position of the group. It is a list of one or several integers separated with commas. $n_1$, the first integer is the number of the group (not nestled in another) in which the sought group is. Inside this group, the second integer $n_2$ is the number of the group (not nestled in another) in which the sought group is\ldots and so on until the necessary nestling depth is reached to obtain the sougth after group.\bigskip Let's take an example with 3 levels of nestled groups. In this example, braces delimiting groups are colored in red for nestling level 1, in blue for level 2 and in green for level 3. The groups are numbered with the rule seen above: \begingroup \def\AccO#1{\text{\color{#1}\ttfamily\{}} \def\AccF#1{\text{\color{#1}\ttfamily\}}} \def\texte#1{\text{\texttt{\color{black}#1}}} \def\decalbrace{\vphantom{\underbrace{A}_A}} \[\texte{a}{\color{red}\underbrace{\AccO{red}{\color{blue}\underbrace{\decalbrace\AccO{blue}\texte{bc}\AccF{blue}}_{\color{blue}1}}\texte{d}{\color{blue}\underbrace{\decalbrace\AccO{blue}\texte{efg}\AccF{blue}}_{\color{blue}2}}\AccF{red}}_{\color{red}1}}\texte{h}{\color{red}\underbrace{\AccO{red}{\color{blue}\underbrace{\decalbrace\AccO{blue}\texte{ij}\AccF{blue}}_{\color{blue}1}}{\color{blue}\underbrace{\decalbrace\AccO{blue}\texte{k}\AccF{blue}}_{\color{blue}2}}{\color{blue}\underbrace{\AccO{blue}\texte{l}{\color{green}\underbrace{\AccO{green}\texte{m}\AccF{green}}_{\color{green}1}}{\color{green}\underbrace{\AccO{green}\texte{no}\AccF{green}}_{\color{green}2}}\AccF{blue}}_{\color{blue}3}}\texte{p}\AccF{red}}_{\color{red}2}}\] \endgroup \smallskip In this example: \nobreak\parindent3em \begin{itemize} \item the group \verb={{bc}d{efg}}= has the identifier: \texttt{\color{red}1} \item the group \verb={ij}= has the identifier: \texttt{{\color{red}2},\color{blue}1} \item the group \verb={no}= has the identifier: \texttt{{\color{red}2},{\color{blue}3},\color{green}2} \item the whole argument \verb=a{{bc}d{efg}}h{{ij}{k}{l{m}{no}}p}= has the identifier \verb=0=, only case where the integer 0 is appears in the identifier of a group. \end{itemize} \parindent0pt \medskip Here is the full example:\par\nobreak\smallskip \showcode+\exploregroups \expandarg \def\chaine{a{{bc}d{efg}}h{{ij}{k}{l{m}{no}}p}} \StrFindGroup{\chaine}{1}[\mongroupe] \meaning\mongroupe\par \StrFindGroup{\chaine}{2,1}[\mongroupe] \meaning\mongroupe\par \StrFindGroup{\chaine}{2,3,2}[\mongroupe] \meaning\mongroupe\par \StrFindGroup{\chaine}{2,5}[\mongroupe] \meaning\mongroupe+ \bigskip The reverse process exists, and several macros of \Xstring provide the identifier of the group in which they made a cut or they found a substring. These macros are: \verb|\IfSubStr|, \verb|\StrBefore|, \verb|\StrBehind|, \verb|\StrSplit|, \verb|\StrLeft|, \verb|\StrGobbleLeft|, \verb|\StrRight|, \verb|\StrGobbleRight|, \verb|\StrChar|, \verb|\StrPosition|.\medskip After these macros, the control sequence \verb|\groupID| expands to the identifier of the group where the cut has been done or the search has succeeded. When not cut can be done or the search fails, \verb|\groupID| is empty. Obvioulsly, the use of \verb|\groupID| has sense only when \verb|\exploregroups| mode is acive and when non starred macros are used.\smallskip Here are some examples with the macro \verb|\StrChar|:\par\nobreak\smallskip \showcode+\exploregroups char 1 = \StrChar{a{b{cd}{e{f}g}h}ijkl}{1}\qquad \string\groupID = \groupID\par char 4 = \StrChar{a{b{cd}{e{f}g}h}ijkl}{4}\qquad \string\groupID = \groupID\par char 6 = \StrChar{a{b{cd}{e{f}g}h}ijkl}{6}\qquad \string\groupID = \groupID\par char 20 = \StrChar{a{b{cd}{e{f}g}h}ijkl}{20}\qquad \string\groupID = \groupID+ \subsection{Splitting a string, the macro {\ttfamily\textbackslash StrSplit}} \label{StrSplit} Here is the syntax:\smallskip \verb|\StrSplit|\ARGU{string}\ARGU{number}\ARGU{stringA}\ARGU{stringB} \smallskip The \argu{string}, is splitted after the \US at position \argu{number}. The left part is assigned to the control sequence \argu{stringA} and the right part is assigned to \argu{stringB}.\par This macro returns two strings, so it does \emph{not} display anything. Consequently, it does not provide the optional argument in last position.\medskip \begin{Conditions} \item If \argu{number}${}\leqslant0$, \argu{stringA} is empty and \argu{stringB} is equal to \argu{string}; \item If \argu{number}${}\geqslant$\argu{lengthString}, \argu{stringA} is equal to \argu{string} and \argu{stringB} is empty; \item If \argu{string} is empty, \argu{stringA} and \argu{stringB} are empty, whatever be the integer \argu{number}. \end{Conditions} \showcode+\def\seprouge{{\color{red}|}} \StrSplit{abcdef}{4}{\csA}{\csB}|\csA\seprouge\csB|\par \StrSplit{a b c }{2}{\csA}{\csB}|\csA\seprouge\csB|\par \StrSplit{abcdef}{1}{\csA}{\csB}|\csA\seprouge\csB|\par \StrSplit{abcdef}{5}{\csA}{\csB}|\csA\seprouge\csB|\par \StrSplit{abcdef}{9}{\csA}{\csB}|\csA\seprouge\csB|\par \StrSplit{abcdef}{-3}{\csA}{\csB}|\csA\seprouge\csB|+ When the exploration of groups is active and the cut is made at the end of a group, the content of the left string will tbe the entire group while the right string will be empty. The example shows this:\par\nobreak\smallskip \showcode+\exploregroups \StrSplit{ab{cd{ef}gh}ij}{6}\strA\strB \meaning\strA\par \meaning\strB+\medskip This macro provides a star version: in this case, the cut is made just before the \US which follows the \US at position \argu{number}. Both version give same results, except when the cut is made at the end of a group; in that case, \verb|\StrSplit| closes as many group as necessary until it finds the next \US: the cut is made just before this \US.\smallskip \showcode+\exploregroups Use without star :\par \StrSplit{ab{cd{ef}gh}ij}{6}\strA\strB \meaning\strA\par \meaning\strB\par \string\groupID\ = \groupID\par\medskip Use with star :\par \StrSplit*{ab{cd{ef}gh}ij}{6}\strA\strB \meaning\strA\par \meaning\strB\par \string\groupID\ = \groupID+ \subsection{Assign a verb content, the macro \ttfamily\textbackslash verbtocs} \label{verbtocs} The macro \verb|\verbtocs| allow to read the content of a \guill{verb} argument containing special characters: \verb|&|, \verb|~|, \verb|\|, \verb|{|, \verb|}|, \verb|_|, \verb|#|, \verb|$|, \verb|^| and \verb|%|. The catcodes of \guill{normal} characters are left unchanged while special characters take a catcode 12. Then, these characters are assigned to a control sequence. The syntax is:\par\nobreak\medskip \hfill\verb|\verbtocs|\ARGU{name}|\argu{characters}|\hfill{} \smallskip \argu{name} is the name of the control sequence receiving, with an \verb|\edef|, the \argu{characters}. \argu{name} thus contains tokens with catcodes 12 (or 10 for space).\medskip By default, the token delimiting the verb content is \guill{|}. Obviously, this token cannot be both delimiting and being contained into what it delimits. If you need to verbatimize strings containing \guill{|}, you can change at any time the token delimiting the verb content with the macro:\par\nobreak\medskip \hfill\verb|\setverbdelim|\ARGU{character}\hfill{}\smallskip Any \argu{token} can be used\footnote{Several tokens can be used, but the syntax of \texttt{\textbackslash verbtocs} becomes less readable ! For this reason, a warning occurs when the argument of \texttt{\textbackslash setverbdelim} contains more than a single token.}. For example, after \verb|\setverbdelim{=}|, a verb argument look like this: \verb|=|\argu{characters}\verb|=|.\medskip About verb arguments, keep in mind that: \begin{itemize} \item all the characters before |\argu{characters}| are ignored; \item inside the verb argument, all the spaces are taken into account, even if they are consecutive. \end{itemize} \medskip Example:\par\nobreak\medskip \showcode+\verbtocs{\resultat} |a & b{ c% d$ e \f| here is the result:\par\resultat+ \subsection{Tokenization of a text to a control sequence, the macro \ttfamily\textbackslash tokenize} The reverse process of what has been seen above is to transform chars into tokens. This is done by the macro:\par\nobreak\medskip \hfill\verb|\tokenize|\ARGU{name}\ARGU{control sequences}\hfill{} \smallskip \argu{control sequences} is fully expanded if \verb|\fullexpandarg| has been called, and is not expanded if \verb|\noexpandarg| or \verb|\expandarg| are active. After expansion, the chars are tokenized to tokens and assigned to \argu{name} with a \verb|\def|.\medskip Example:\par\nobreak\medskip \showcode+\verbtocs{\text}|\textbf{a} $\frac{1}{2}$| text : \text \tokenize{\resultat}{\text}\par result : \resultat+ \medskip Obviously, the control sequence \verb|\result| can be called at the last line since the control sequences it contains are defined. \subsection{Expansion of a control sequence before verbatimize, the macros {\ttfamily\textbackslash StrExpand} and {\ttfamily\textbackslash scancs}} \label{scancs} The macro \verb|\StrExpand| expands the tokens of a string:\par\nobreak\medskip \verb|\StrExpand|\arguC{number}\ARGU{string}\ARGU{name}\smallskip By default, \argu{number} is 1 and represnts the number of times each token of \argu{string} has to be expanded. The \argu{name} is a control sequence receiving the result of the expansions.\medskip Macro works sequentially and by pass: in a pass, each token from left to right is replaced by its 1-expansion. After this expansion, if the \argu{number} of expansions is not reached, an other pass starts, and so on.\medskip Here is an example:\par\nobreak\smallskip \showcode+\def\csA{1 2} \def\csB{a \csA} \def\csC{\csB\space} \def\csD{x{\csA y}\csB{\csC z}} Expansion of \string\csD\ au\par \StrExpand[0]{\csD}{\csE} level 0 : \detokenize\expandafter{\csE}\par \StrExpand[1]{\csD}{\csE} level 1 : \detokenize\expandafter{\csE}\par \StrExpand[2]{\csD}{\csE} level 2 : \detokenize\expandafter{\csE}\par \StrExpand[3]{\csD}{\csE} level 3 : \detokenize\expandafter{\csE}\par \StrExpand[4]{\csD}{\csE} level 4 : \detokenize\expandafter{\csE}+ \medskip The macro expands each token consecutively, and does not see what follows: tokens whose expansion depends on other tokens cannot be expanded with this macro. For instance, though "\hbox{\verb|\iftrue A\else B\fi|}" has a natural expansion ("A"), it cannot be put in the argument of \verb|\StrExpand| and:\par\nobreak\smallskip \hfil\verb|\StrExpand{\iftrue A\else B\fi}\result|\hfil{}\par\nobreak\smallskip provokes an error because the first token "\verb|\iftrue|" is expanded \emph{alone} without seeing its \verb-\fi- which makes \TeX{} angry. Expansion inside groups is \emph{independant} of the exploration mode: this macro has its own command to expand or not what is inside the groups. By default, tokens inside groups are expanded, but this can be changed with the macro \verb-\noexpandingroups-. The default behaviour can be recoverd with \verb-\expandingroups-.\bigskip The \verb-\scancs- macro returns the detokenized result:\par\nobreak\medskip \verb|\scancs|\arguC{number}\ARGU{name}\ARGU{string}\par\nobreak\smallskip The \argu{number} is 1 by default and represents the number of expansions.\smallskip \verb-\scancs- has been kept for compatibility with older versions of \Xstring. This macro now unnecessry, simply takes the result of \verb|\StrExpand| and \verb|\detokenize| it. \subsection{Inside the definition of a macro} Some difficulties arise inside the definition of a macro, i.e. between braces following a \verb|\def\macro| or a\\\verb|\newcommand\macro|.\medskip It is forbidden to use the command \verb|\verb| inside the definition of a macro. For the same reasons:\par\medskip \hfill\textbf{Do not use \texttt{\textbackslash verbtocs} inside the definition of a macro}.\hfill{}\medskip But then, how to manipulate special characters and "verbatimize" inside the définition of macros ?\bigskip The \verb|\detokenize| primitive of $\varepsilon$-\TeX can be used but it has limitations: \begin{itemize} \item braces must be balanced; \item consecutive spaces make a single space; \item the \verb|%| sign is not allowed; \item a space is inserted after each control sequence; \item \verb|#| signs become \verb|##|. \end{itemize} \medskip It is better to use \verb|\scancs| and define \emph{outside the definition of the macros} control sequences containing special characters with \verb|\verbtocs|. It is also possible to use \verb|\tokenize| to transform the final result (which is generaly \texte{10,11,12}) into control sequences.\medskip In the following teaching example\footnote{It is possible to make much more simple using \texttt{\textbackslash detokenize}. The macro becomes:\par\texttt{\textbackslash newcommand\textbackslash bracearg[1]\{\textbackslash detokenize\{\{\#1\}\}\}}}, the macro \verb|\bracearg| adds braces to its argument. To make this possible, 2 control sequences \verb|\Ob| and \verb|\Cb| containing "\verb|{|" and "\verb|}|" are defined outside the definition of \verb|\bracearg|, and expanded inside it:\par\nobreak\medskip \showcode+\verbtocs{\Ob}|{| \verbtocs{\Cb}|}| \newcommand\bracearg[1]{% \def\text{#1}% \scancs{\result}{\Ob\text\Cb}% \result} \bracearg{xstring}\par \bracearg{\a}+ \subsection{The macro \ttfamily\textbackslash StrRemoveBraces} Advanced users may need to remove the braces of an argument. The macro \verb|\StrRemoveBraces| does this. Its syntax is:\smallskip \verb|\StrRemoveBraces|\ARGU{string}\arguC{name} \smallskip This macro is sensitive to exploration mode and will remove \emph{all} the braces with \verb|\exploregroups| while it will remove braces of lower level with \verb|\noexploregroups|.\medskip \showcode+\noexploregroups \StrRemoveBraces{a{b{c}d}e{f}g}[\mycs] \meaning\mycs \exploregroups \StrRemoveBraces{a{b{c}d}e{f}g}[\mycs] \meaning\mycs+ \begin{center} $\star$\par $\star$\quad$\star$ \end{center} That's all, I hope you will find this package useful!\par\nobreak Please, send me an \href{mailto:unbonpetit@netc.fr}{email} if you find a bug or if you have any idea of improvement\ldots\par\nobreak\medskip Christian Tellechea \end{document}