此函数的操作类似于index,不同之处在于它返回STR中最后一次出现SUBSTR的位置。如果指定了POSITION,则返回该位置或该位置之前的最后一次出现。
以下是此函数的简单语法-
rindex STR, SUBSTR, POSITION rindex STR, SUBSTR
该函数在失败时返回undef,否则返回最后一次出现的位置。
以下是显示其基本用法的示例代码-
链接:https://www.learnfk.comhttps://www.learnfk.com/perl/perl-rindex.html
来源:LearnFk无涯教程网
#!/usr/bin/perl -w $pos = rindex("abcdefghijiklmdef", "def"); print "Found position of def $pos\n"; # Use the first position found as the offset to the # next search. # Note that the length of the target string is # subtracted from the offset to save time. $pos = rindex("abcdefghijiklmdef", "def", $pos-3 ); print "Found position of def $pos\n";
执行上述代码后,将产生以下输出-
Found position of def 14 Found position of def 3
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)