I am trying to replace, in a given string, some characters. The characters to be replaced is given by its ASCII code. So for example, in abc, I want to replace character 97 (which is a, but I don't know this yet) by something else, let's say a dot (.).
Now, a, \char97 and \char\thenumber all look the same to me when printing them in a document, but replacing this respective argument in a string a only works with the first. Why is that, and how can I solve it? This is my MWE (unchanged from the original question):
\documentclass{book}
\usepackage{xstring}
\newcounter{number}
\setcounter{number}{97}
\newcommand{\replace}[1]{\StrSubstitute{#1}{a}{.}}
\begin{document}
a
\char97
\char\thenumber
\replace{a}
\expandafter\replace{\char97}
\expandafter\expandafter\expandafter\replace{\char\thenumber}
\end{document}
\char97is not the same asa: the former is three tokens, the latter just one. There are situations whereaand\char97might result in very different output (math mode, in particular). The construction\expandafter\replace\expandafter{\char97}can't work (in yours\expandafterdoes nothing) because\charis not expandable. – egreg Aug 10 '14 at 19:25\char\value{number}or\symbol{\value{number}}is much better than\char\thenumber, because\thenumbercan expand into something that is not a number. And if it expands to a number, then it does not stop for looking digits in the following text. – Heiko Oberdiek Aug 10 '14 at 19:29\number`a? – Joseph Wright Aug 10 '14 at 19:32\expandafterdoes only expand the token after the next token, thus the example expands the unexpandable{three times in summary. And even if you insert\expandafterafter\replace,\charis not expandable. – Heiko Oberdiek Aug 10 '14 at 19:34\charis not expandable. That's the information I was missing. No,\numberis not was I was looking for: I wanted to use\StrSubstitute. – bers Aug 10 '14 at 19:35\number`areturns97, I do not see how this is helping. Yes,\char\number`aoutputsa, but: 1. I want to replace to list of characters given by ASCII codes in a string (so I already have the result of\number), and 2.\replace{\char\number`a}does not work in my MWE either. – bers Aug 11 '14 at 05:35\StrSubstitute{#1}{\char97}{.}? – Manuel Aug 11 '14 at 10:59