To find the index of the last occurrence of a substring in Perl, you can use the rindex
function.
In this example,
$str
with a string value.rindex
function to find the index of the last occurrence of a specific substring within $str
.rindex
function returns the index of the last occurrence of the substring in the string.$str = "Hello World, Hello Perl";
$last_index = rindex($str, 'H');
print "Last index of substring is: $last_index\n";
Last index of substring is: 14
In this example,
$str1
and $str2
with different values.rindex
function to find the index of the last occurrence of $str2
within $str1
.rindex
function returns the index of the last occurrence of $str2
in $str1
.$str1 = "apple banana cherry banana";
$str2 = "banana";
$last_index = rindex($str1, $str2);
print "Last index of substring is: $last_index\n";
Last index of substring is: 17
In this tutorial, we learned How to find the Index of Last Occurrence of Substring in Perl language with well detailed examples.