You are viewing the version of this documentation from Perl blead. This is a development version of Perl.
index STR,SUBSTR,POSITION
index STR,SUBSTR

The index function searches for one string within another, but without the wildcard-like behavior of a full regular-expression pattern match. It returns the position of the first occurrence of SUBSTR in STR at or after POSITION. If POSITION is omitted, starts searching from the beginning of the string. POSITION before the beginning of the string or after its end is treated as if it were the beginning or the end, respectively. POSITION and the return value are based at zero. If the substring is not found, index returns -1.

Find characters or strings:

index("Perl is great", "P");     # Returns 0
index("Perl is great", "g");     # Returns 8
index("Perl is great", "great"); # Also returns 8

Attempting to find something not there:

index("Perl is great", "Z");     # Returns -1 (not found)

Using an offset to find the second occurrence:

index("Perl is great", "e", 5);  # Returns 10