📜  珀尔 | rindex()函数

📅  最后修改于: 2022-05-13 01:55:34.848000             🧑  作者: Mango

珀尔 | rindex()函数

Perl 中的 rindex()函数的操作类似于 index()函数,不同之处在于它返回字符串(或文本)中子字符串(或模式)最后一次出现的位置。如果指定了位置,则返回该位置或之前的最后一次出现。

示例 1:

#!/usr/bin/perl -w
  
$pos = rindex("WelcomeToGeeksforGeeksWorld", "eks");
print "Position of eks: $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("WelcomeToGeeksforGeeksWorld", 
                          "eks", $pos - 3 );
print "Position of eks: $pos\n";
输出:
Position of eks: 19
Position of eks: 11

示例 2:

#!/usr/bin/perl -w
  
$pos = rindex("GeeksforGeeks", "eks");
print "Position of eek: $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("GeeksForGeeks", "eks", $pos - 2);
print "Position of eek: $pos\n";
输出:
Position of eek: 10
Position of eek: 2