Text Formattingxstring last index of

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
batnas
Posts: 1
Joined: Thu Dec 20, 2012 1:30 pm

xstring last index of

Post by batnas »

Hey everyone!

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}}}
but it seems that StrCount is not returning an integer that StrPosition can take as input, as the code always fail. However, if I replace

Code: Select all

\renewcommand\numberofsubstr{\StrCount{#1}{#2}}%
with
\renewcommand\numberofsubstr{2}%
it works, bu then it is not dynamic/generic.

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}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

xstring last index of

Post by cgnieder »

Hi batnas,

Welcome to the LaTeX community!

Strictly speaking there are no functions in TeX and hence there is no returning either...

What you want is \StrCount's optional argument. In it you give a control sequence. The number obtained by \StrCount will then be stored in this macro. Most of xstring's commands have this optional argument, see the documentation (xstring) for details.

In the example below I used an internal macro (marked by the use of @ in its name).

Code: Select all

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{xstring}

% make sure \lastsubstrposition has not been defined before:
\newcommand\lastsubstrposition{}

\makeatletter% make @ a letter
\newcommand\lastindex[2]{%
  \StrCount{#1}{#2}[\number@of@substr]%
  \StrPosition[\number@of@substr]{#1}{#2}[\lastsubstrposition]}%
\makeatother% make @ an other token again

\begin{document}

\lastindex{Find the.last dot.in this string}{.}

last index is \lastsubstrposition...

\end{document}
Regards
site moderator & package author
Post Reply