I want to split a character string n by n. I know how to do that 1 by 1 :
\documentclass{article}
\makeatletter
\def\s@plit<#1#2>{%
\ifx\empty#2%
#1%
\else
#1,\s@plit<#2>%
\fi}
\def\Split#1#2{\s@plit<#2\empty>}
\makeatother
\begin{document}
\Split{1}{123456789}
\end{document}
It gives : 1,2,3,4,5,6,7,8,9
But I also want 1,23,45,67,89 or 123,456,789 ... depending from the first parameter.
EDIT
Work straight and reverse, even with foreach.

\documentclass{article}
\usepackage{tikz,xparse,xstring}
\DeclareDocumentCommand{\split}{smmo}{%
\xdef\Entree{#3}%
\let\Sortie\empty%
\IfBooleanTF #1
{% sans étoile
\loop
\StrRight{\Entree}{#2}[\tmp]%
\edef\Sortie{\Sortie,\tmp}%
\StrGobbleRight{\Entree}{#2}[\Entree]%
\unless\ifx\Entree\empty
\repeat
\StrGobbleLeft{\Sortie}{1}[\Sortie]%
}
{% avec étoile dans l'ordre inverse
\loop
\StrRight{\Entree}{#2}[\tmp]%
\edef\Sortie{\tmp,\Sortie}%
\StrGobbleRight{\Entree}{#2}[\Entree]%
\unless\ifx\Entree\empty
\repeat
\StrGobbleRight{\Sortie}{1}[\Sortie]%
}
\IfNoValueTF{#4}{\Sortie}{\edef#4{\Sortie}}
}
\begin{document}
\split{2}{6513782}
\split*{2}{6513782}
\split*{2}{6513782}[\bob]
\foreach \x in \bob {[\x] }
\end{document}

