Replacing a substring
\documentclass{article}
\usepackage{xstring}
\def\ReplaceStr#1{%
\IfSubStr{#1}{XY}{%
\StrSubstitute{#1}{XY}{$\to$}}{#1}}
\begin{document}
\ReplaceStr{aXYbXYc}
\ReplaceStr{abc}
\end{document}
\documentclass{article}
\usepackage{listofitems}
\newcommand\substr[3]{%
\setsepchar{#2}%
\readlist\parsedinput{#1}%
\foreachitem\x\in\parsedinput{%
\ifnum\xcnt=1\else#3\fi\x%
}%
}
\begin{document}
\substr{aXYbXYc}{XY}{$\to$}
\end{document}