perl function rindex
The rindex
function in Perl is used to find the last occurrence of a substring within a string, and return the position of the last character of the substring.
Here's an example of using rindex
:
my $string = "The quick brown fox jumps over the lazy dog"; my $position = rindex $string, "fox"; print "$position\n"; # prints 16
In this example, the rindex
function is used to find the position of the last occurrence of the substring "fox" within the string $string
. The function returns the position of the last character of the substring, which is 16.
If the substring is not found in the string, the function returns -1. Here's an example:
my $string = "The quick brown fox jumps over the lazy dog"; my $position = rindex $string, "cat"; print "$position\n"; # prints -1
In this example, the rindex
function is used to find the position of the last occurrence of the substring "cat" within the string $string
. Since "cat" is not found in the string, the function returns -1.
Note that rindex
is case sensitive. If you want to perform a case-insensitive search, you can convert the string and the substring to lowercase or uppercase before calling rindex
.