[Tex/LaTex] Process a string with replacing and splitting

macrosxstring

I have an input string like "123-45" or "1-3-5" or "2-4; 6-8" where the minus signed should be removed. This is easy with the xstring package.

\StrSubstitute{#1}{-}{}

The next step is to split the input strings at the semicolon. This maybe could be done with \StrMid. The final output should be than:

123-45 = \somecommand{12345}
1-3-5 = \somecommand{135}
2-4 = \somecommand{24}
6-8 = \somecommand{68}

At the moment I have this not real working code:

\newcommand{\test}[1]{%
  \newcommand{\stripped}{\StrSubstitute{#1}{-}{}}%
  \IfInteger{\stripped}{\somecommand{\stripped}}{error}%
}

How can I solve this issue?

Best Answer

You can also use the xstring package:

\documentclass[a4paper, 11pt]{article}

\usepackage{xstring}
\def\somecommand#1{\textbf{#1}\par}
\makeatletter
\newcommand{\test}[1]{%
  \StrSubstitute{#1}{-}{}[\stripped]%
  \StrCount{\stripped}{;}[\@tempcnta]
  \ifnum\@tempcnta=0
    %no semicolon
     \somecommand{\stripped}
  \else
   %semicolon
    \@test{\stripped}%
  \fi
}
\def\@test#1{%
\StrBefore{#1}{;}[\@tempa]
\StrBehind{#1}{;}[\@tempb]
\somecommand{\@tempa}
\test{\@tempb}
}
\begin{document}
\test{2-4; 6-8}

\test{123-45}
\end{document}