# Substitute string that contains a backslash

I need to create a function that will transform a string such as:

Hello\textbackslash{}Bye


into this:

Hello \\ Bye


I have tried using StrSubstitute (from the xstring package) without success. I have attempted things along these lines:

\newcommand{\TRANSFORM}[1]{\StrSubstitute{#1}{\textbackslash{}}{ \noexpand\\ }}

\TRANSFORM{Hello\textbackslash{}Bye}


You could just temporarily redefine the meaning of \textbackslash, letting it to \\:

\documentclass{article}

\newcommand{\TRANSFORM}[1]{{\let\textbackslash\\#1}}
\setlength{\parindent}{0pt}% Just for this example

\begin{document}

Hello\textbackslash{}Bye

\TRANSFORM{Hello\textbackslash{}Bye}

\end{document}


You need to use the \noexpandarg mode of xstring, so it won't try expanding its arguments:

\documentclass{article}
\usepackage{xstring}

%\noexpandarg % set \noexpandarg globally

\newcommand{\TRANSFORM}[1]{%
\saveexpandmode\noexpandarg % set \noexpandarg locally
\StrSubstitute{#1}{\textbackslash{}}{\\}%
\restoreexpandmode % restore the previous mode
}

\begin{document}

\TRANSFORM{Hello\textbackslash{}Bye}

\end{document}


Depending on your usage of xstring, you might also set \noexpandarg globally (by commenting/uncommenting the relevant lines).

Of course, in this case, redefining a macro is better:

\newcommand\TRANSFORM[1]{{% open a group
\renewcommand{\textbackslash}[1]{\\}%
#1%
}}


Why \renewcommand and not \let? Because this redefinition also swallows {}.