The problem seems to rely in two settings that you should apply:
\noexpandargs
\exploregroups
If I give your definitions and use those declarations, from
\changeboth{A/B\$C}
I get that LaTeX prints
A5cB24C
which seems what you're expecting. However you'll have no way to retrieve the modified string. If instead you want to store the result in a macro for later usage, a different approach should be used.
\documentclass{article}
\usepackage{xstring} % for StrSubstitute
\newcommand*{\changeslash}[1]{%
\StrSubstitute{#1}{/}{5c}[\temp]%
}
\newcommand*{\changedollars}[1]{%
\StrSubstitute{#1}{\$}{24}[\temp]%
}
\newcommand*{\changeboth}[1]{%
\changedollars{#1}%
\expandafter\changeslash\expandafter{\temp}%
}
\begin{document}
\noexpandarg
\changeboth{A/B\$C}
\show\temp
\end{document}
This will produce no output, but TeX will print on the terminal
> \temp=macro:
->A5cB24C.
l.18 \show\temp
and you can freely use the changed string. Here \exploregroups
is not needed.
A different approach uses LaTeX3 features.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
% Document commands
\NewDocumentCommand{\changeslash}{ m }
{
\baitisj_change_slash:n { #1 }
}
\NewDocumentCommand{\changedollars}{ m }
{
\baitisj_change_dollars:n { #1 }
}
\NewDocumentCommand{\printchangedstring}{}
{
\tl_use:N \l_baitisj_string_tl
}
\NewDocumentCommand{\changeboth}{ m }
{
\baitisj_change_dollars:n { #1 }
\baitisj_change_slash:V \l_baitisj_string_tl
}
% Variables and functions
\tl_new:N \l_baitisj_string_tl
\cs_new_protected:Npn \baitisj_change_slash:n #1
{
\tl_set:Nn \l_baitisj_string_tl { #1 }
\tl_replace_all:Nnn \l_baitisj_string_tl { / } { 5c }
}
\cs_new_protected:Npn \baitisj_change_dollars:n #1
{
\tl_set:Nn \l_baitisj_string_tl { #1 }
\tl_replace_all:Nnn \l_baitisj_string_tl { \$ } { 24 }
}
\cs_generate_variant:Nn \baitisj_change_slash:n { V }
\ExplSyntaxOff
\begin{document}
\changeboth{A/B\$C}
\printchangedstring
\end{document}
The scheme for extending this to more substitutions seems clear. It depends on your application using \printchangedstring
or using the modified string in other ways.
Note In any case (xstring
or xparse
method), if you need to change the literal $
into 24
, don't use \$
, but simply $
.