当前位置:首页 » Perl » perl rindex

perl rindex

perl rindex函数例子,rindex函数实例代码 - 操作类似index,但它返回SUBSTR在STR中最后一次出现的位置。

语法

rindex STR, SUBSTR, POSITION

rindex STR, SUBSTR


定义和用法

操作类似index,但它返回SUBSTR在STR中最后一次出现的位置。如果指定位置POSITION ,返回最后一次出现在该位置之前。

返回值

  • undef - 失败时

  • 否则的最后一次出现的位置

例子

试试下面的例子:

#!/usr/bin/perl -w
#by www.gitbook.net

$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