# [texhax] Last char of a string

Michael Barr barr at math.mcgill.ca
Thu Jul 7 18:11:25 CEST 2011

No, I am not using pdftex.  For the time being the journal archives
everything as dvi, along with ps and pdf.  But someone pointed me to
xstring which has a procedure called IfEndWith and that works.

Michael

On Thu, 7 Jul 2011, Paul Isambert wrote:

> 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
>
>