Using the Perl index() function

Using the Perl index() function

Introduction

The index() function is used to determine the position of a letter or a substring in a string. For example, in the word “frog” the letter “f” is in position 0, the “r” in position 1, the “o” in 2 and the “g” in 3. The substring “ro” is in position 1. Depending on what you are trying to achieve, the index() function may be faster or more easy to understand than using a regular expression or the split() function.

Example 1a. Where in a string is a certain letter?

Let’s say you are trying to determine if a word has a particular letter in it. Let’s look for the letter “l” in the string “perlmeme.org”.

This program gives you:

Example 1b. The string doesn’t contain the letter?

If the string doesn’t contain the letter, index() will return a -1. For example, we can look for the letter “L” in the string “perlmeme.org”:

The program outputs:

Example 1c. The string contains more than one of the letter?

If the letter we’re searching for appears more than once in the string, index() return the index of the first occurrence of the letter.

This program gives you:

Example 2. Looking for a substring

Looking for a substring (rather than a single character) works in exactly the same way as looking for a single character:

This program gives you:

Example 3a. What if I don’t want the first occurrence?

The index() function let’s you specify an offset. This tells index() where to start looking in the string. In our example, we found the first occurrence of the letter ‘e’ at position 1. Let’s try to find the second:

The program outputs:

Example 3b. How to find every occurrence

To find (and do something with) every occurrence of a character in a string, you could use index() in a loop, incrementing the offset each time:

When we run this program, we get the following output:

Example 4. How to find the last occurrence

Instead of looping through every occurrence of a letter in a string to find the last one, you can use the rindex() function. This works exactly the same as index() but it starts at the end of the string. The index value returned is string from the start of the string though.

This would produce:

For more information on rindex() see our rindex() howto.

See also

Scroll to Top