当前位置: 首页 > 文档资料 > Perl 入门教程 >

rindex

优质
小牛编辑
116浏览
2023-12-01

描述 (Description)

此函数的操作类似于index,除了它返回STR中最后一次出现的SUBSTR的位置。 如果指定了POSITION,则返回该位置或之前的最后一次出现。

语法 (Syntax)

以下是此函数的简单语法 -

rindex STR, SUBSTR, POSITION
rindex STR, SUBSTR

返回值 (Return Value)

此函数在失败时返回undef,否则返回上次出现的位置。

例子 (Example)

以下是显示其基本用法的示例代码 -

#!/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