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 0index("Perl is great", "g"); # Returns 8index("Perl is great", "great"); # Also returns 8Attempting to find something not there:
index("Perl is great", "Z"); # Returns -1 (not found)Using an offset to find thesecond occurrence:
index("Perl is great", "e", 5); # Returns 10Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.
The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.