comparing substrings

I would like to get the following code running, but the second test fails with

! Argument of \@secondoftwo has an extra }.


What do I miss?

\documentclass{article}
\usepackage{xstring,ifthen}
\newcommand{\mytest}[1]{%
\newcommand{\s}{\StrChar{#1}{3}}
\newcommand{\x}{c}
3rd charactor of input is: \s ~equal to \x \\
1st test: \IfStrEq{\x}{c}{true}{false} \\
2nd test: \IfStrEq{\s}{c}{true}{false}
}
\begin{document}
\mytest{abcd}
\end{document}


Any help appreciated, thanks, Malte

You're not defining \s to be the third character, but rather to be the complicated set of instructions for printing the third character.

Use better xstring features:

\documentclass{article}
\usepackage{xstring}
\newcommand{\mytest}[1]{%
\StrChar{#1}{3}[\maltetest]%
Third charactor of input is: \maltetest\ equals c\\
Test: \IfStrEq{\maltetest}{c}{true}{false}%
}
\begin{document}
\mytest{abcd}
\end{document}


This way \maltetest will exactly contain the third character of the input string.

• Many many thanks, egreg, - that's so simple :-) – Malte Feb 22 '18 at 15:53