Locating the last instance of a string

To locate the last instance of a string for an exact match, Tcl provides the keyword last. Let's look at the syntax to illustrate how this can be used.

The syntax for the string command is as follows:

	string last string1 string2 index

When invoked with the last command, the string command locates the last instance of string1 contained within string2.

How to do it…

In the following example, we locate the last instance of a string contained within another string. Enter the following command:


% string last abc abcabcabc
6

How it works…

As you can see, string has returned 6, indicating that the last exact match of the search phrase is located at the index of 6. When invoked with the last keyword, the string command ...

Get Tcl/Tk 8.5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.