Perl 中的 rindex函数

首页 / Perl入门教程 / Perl 中的 rindex函数

描述

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

语法

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

rindex STR, SUBSTR, POSITION

rindex STR, SUBSTR

返回值

该函数在失败时返回undef,否则返回最后一次出现的位置。

无涯教程网

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

链接:https://www.learnfk.comhttps://www.learnfk.com/perl/perl-rindex.html

来源:LearnFk无涯教程网

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Android开发高手课 -〔张绍文〕

软件工程之美 -〔宝玉〕

Java性能调优实战 -〔刘超〕

浏览器工作原理与实践 -〔李兵〕

编译原理实战课 -〔宫文学〕

爱上跑步 -〔钱亮〕

徐昊 · TDD项目实战70讲 -〔徐昊〕

高并发系统实战课 -〔徐长龙〕

结构学习力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)