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