# [texhax] Last char of a string

Paul Isambert zappathustra at free.fr
Thu Jul 7 17:07:44 CEST 2011

Le 07/07/2011 15:05, Michael Barr a écrit :
> Is there any easy way to find the last character of a string?
>
> Why?  Well the online journal I do the tex editing for uses runin
> subsection titles.  They have to end with a period or other
> punctuation. So I want to look at the last character of the title and
> add a period unless it already is a period (or question mark or even
> an exclamation point).

You're probably using pdfTeX, even if you don't know it. So there is
\pdfmatch{<pattern>}{<expression>}

%%%
\def\first#1#2{#1} \def\second#1#2{#2}
\def\ifendwith#1#2{%
\ifnum\pdfmatch{[#1]\$}{#2} = 1
\expandafter\first
\else
\expandafter\second
\fi
}

\ifendwith{?}{abc?}{true}{false}
%%%

(I put the character to test between brackets so it isn't considered as
a metacharacter, e.g. with "?" or ".".)

Best,
Paul