Perl rindex 函式



描述

該函式的操作與 index 類似,但它返回 SUBSTR 在 STR 中最後一次出現的位置。如果指定了 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_function_references.htm
廣告
© . All rights reserved.