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