# Replacing a substring

I've got a string like this: aXYbXYc, and need to replace the XY substring with a right arrow: a $\to$ b $\to$ c.

Is there any function (also provided by external packages) to help me do that?

• Like sed? E.g., sed 's/$$[a-z]$$XY/\1 $\\to$ /g' myfile.tex... – jon Mar 30 '17 at 1:51

\documentclass{article}
\usepackage{xstring}
\def\ReplaceStr#1{%
\IfSubStr{#1}{XY}{%
\StrSubstitute{#1}{XY}{$\to$}}{#1}}

\begin{document}

\ReplaceStr{aXYbXYc}

\ReplaceStr{abc}

\end{document}

• Please don't use minimal for examples. – cfr Mar 29 '17 at 22:43
• What is the point in using \IfSubStr? – Solomon Ucko Jan 5 at 2:36
• I cannot remember, so I must say it is without any sense ... – user2478 Jan 5 at 15:11
\documentclass{article}
\usepackage{listofitems}
\newcommand\substr[3]{%
\setsepchar{#2}%
\substr{aXYbXYc}{XY}{$\to$}