[Tex/LaTex] Remove spaces from macro argument

macros

I'm working with some generated LaTeX code, which contains code like this:

\command{a, b}

I supply the definition of \command. It's internals are not really relevant to the question, but most importantly it's passing the argument to another command \backend (which cannot be changed by me):

\newcommand{\command}[1]{\backend{#1}}

Now my issue is that \backend fails with an argument such as a, b because it cannot handle whitespace after that comma.

How can I remove that (single) whitespace after each comma in an argument passed to a macro? Is it possible to do so with another TeX macro?

Best Answer

Just another solution that works (apart from the one linked by campa):

\documentclass{article}
\usepackage{expl3,xparse}
\NewDocumentCommand{\backend}{m}{#1}
\ExplSyntaxOn
\NewDocumentCommand{\command}{m}{
    \tl_set:Nn \l_tmpa_tl {#1}
    \tl_replace_all:Nnn \l_tmpa_tl { ~ } { }
    \backend{\tl_use:N\l_tmpa_tl}
}
\ExplSyntaxOff
\begin{document}
\command{My space is useless!}
\end{document}

Update: As suggested in the comments now a version with expansion (and with correction of the typo [forgotten comma]).

\documentclass{article}
\usepackage{expl3,xparse}
\NewDocumentCommand{\backend}{m}{#1}
\ExplSyntaxOn
\NewDocumentCommand{\command}{m}{
    \tl_set:Nn \l_tmpa_tl {#1}
    \tl_replace_all:Nnn \l_tmpa_tl { ,~ } { , }
    \exp_args:No \backend { \l_tmpa_tl }
}
\ExplSyntaxOff

\def\foo{quack}

\begin{document}
\command{My space, is useless!, \foo}
\end{document}
Related Question