I have been trying to create a "function" to give me the last (essentially the nth) index of a string within a string. I am using xstring, which provides me with the most basic string-functions, but the does not seem to be a function that does what I want.
What I currently have is:
Code: Select all
\newcommand\lastindexof[2]{%
\renewcommand\numberofsubstr{\StrCount{#1}{#2}}%
\renewcommand\lastsubstrposition{\StrPosition[ \numberofsubstr ]{#1}{#2}}}
Code: Select all
\renewcommand\numberofsubstr{\StrCount{#1}{#2}}%
with
\renewcommand\numberofsubstr{2}%
I am quite new to LaTeX-programming, so there may be something fundamentally wrong with the code, and there may also be a smarter/better way of doing it...
I hope someone can help me...
\\Batnas
PS:
Heres a MWE showing my problem...
Code: Select all
% !TeX spellcheck = da_DK
\documentclass[a4paper,12pt]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%
\usepackage{xstring}
\newcommand\numberofsubstr{}
\newcommand\lastsubstrposition{}
\newcommand\lastindex[2]{%
\renewcommand\numberofsubstr{\StrCount{#1}{#2}}%
\renewcommand\numberofsubstr{2}% if you comment this line, and thus supplying the result of StrCount, pdflatex will fail...
\renewcommand\lastsubstrposition{\StrPosition[ \numberofsubstr ]{#1}{#2}}}%
\begin{document}
\lastindex{Find the.last dot.in this string}{.}
last index is \lastsubstrposition...
\end{document}