Perl index 函数



描述

此函数从开头(从零开始)或从指定的 POSITION 开始,返回 STR 中 SUBSTR 首次出现的的位置。

语法

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

index STR, SUBSTR, POSITION

index STR, SUBSTR

返回值

此函数在失败时返回 -1,否则返回匹配字符串的位置(第一个字符的位置从零开始)。

示例

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

#!/usr/bin/perl

$string = "That is test";

$index = index ($string, 'is');

print "Position of is in the string $index\n";

当执行上面代码时,它会产生以下结果 -

Position of is in the string 5
perl_function_references.htm
广告
© . All rights reserved.