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}
Best Answer
There is a standard
\lowercase
trick which creates a token with a given ASCII code. For example you need to replace all ASCII 97 characters from the stringabcabcac
by double?
. Then you can try:This code creates the token with given ASCII code instead the
X
letter and then the\StrSubstitute
macro is executed.Of course, your processed string cannot include the
X
letter itself. If this is a problem then you can create the following\replace
macro: