當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Perl rindex用法及代碼示例


描述

此函數的操作與 index 類似,隻是它返回 STR 中最後一次出現 SUBSTR 的位置。如果指定了 POSITION,則返回該位置或該位置之前的最後一次出現。

用法

以下是此函數的簡單語法 -

rindex STR, SUBSTR, POSITION

rindex STR, SUBSTR

返回值

此函數在失敗時返回 undef,否則返回最後一次出現的位置。

示例

以下是顯示其基本用法的示例代碼 -

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

相關用法


注:本文由純淨天空篩選整理自 Perl rindex Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。