I read a file line to \a. Say \a equals A Q B with a space before A and after B.
Then I use \StrBetween[2,3]{\a}{ }{ }[\itstr], so that \itstr equals Q.
Now \IfStrEq{\itstr}{Q}{true}{false} should be true, but it is not. Would you help me with this please.
1 Answers
If letters or strings should be read and compared later with xstring macros \read should be used instead of \readline, the latter detokenizing (i.e. changing the catcodes to 12 for all characters except of character 32 which will get catcode 10 the content, which is not wanted here. \read maintains the catcodes, so characters belonging to 'letter' will have this category later on still.
Also explicit \space must be used instead of empty {} or { } in order to match the content.
\documentclass[12pt]{article}
\usepackage{xstring}
\begin{document}
\newdimen\qsum
\newread\Karnameh
\immediate\openin\Karnameh=Karnameh
\read\Karnameh to \a
\closein\Karnameh
\StrBetween[6,7]{\a}{\space}{\space}[\itstr]%
\fbox{\itstr}
\IfStrEq{\itstr}{Q}{\global\qsum=1pt}{\global\qsum=5pt}
qsum=\the\qsum
\end{document}
Please note that \read will also grab the \endlinechar here as well, but let us ignore this for a moment.
This will give 1pt for the \qsum dim register.
File Karmaneh
StNumber LastName FirstName Dotssss D Q Q Q A A A Q A Examm A Q A A Q Q A A Examm Q Q Q A A A A Q A Q Examm Examm ExCr
Update
If \readline is a 'must', use an explicit \detokenize for the Q character that is handled to the \IfStrEq macro.
\IfStrEq{\itstr}{\detokenize{Q}}{\global\qsum=1pt}{\global\qsum=5pt}

\documentclass{article} \usepackage{xstring} \begin{document}...\end{document}– Sep 09 '17 at 06:23\StrBetween[1,2]{\a}{\space}{\space}[\itstr]rather, in order to catch the content between the first (1) and second (2) occurcence of the explicit space tokens in the string, so\itstrshould expand toQ? – Sep 09 '17 at 06:30\begin{document} \newdimen\qsum \newread\Karnameh\immediate\openin\Karnameh=Karnameh \readline\Karnameh to \a \StrBetween[6,7]{ \a }{ }{ }[\itstr] itstr=$|\itstr|$ \IfStrEq{\itstr}{Q}{\global\qsum=1pt}{\global\qsum=5pt} qsum=\the\qsum\end{document}
– Naser Sep 09 '17 at 06:41\readlineto\readand use\spaceas suggested above – Sep 09 '17 at 07:08