1. index 函数
index 主要用于字符串查找,返回从左->右查到子字符串的起始位置(起始位置0) ,可以带括号,也可以不带。当找不到会返回-1
使用方法:
index STR,SUBSTR,POSITION
index STR,SUBSTR
实例:
#!/usr/bin/perl
use strict;
my $str1="Love me, love my dog\n";
print "return the first child string location\n";
print index $str1,"ove";
print "\nreturn the first child string from start postition\n";
print index($str1,"ove",2);
print "\nif can't find return -1\n";
print index($str1,"LOVE");
print "\n";
结果:
D:\>perl index.pl
return the first child string location 查找ove, ove 总共有出现第一次出现1,第二次10
1
return the first child string from start postition
10 <-----------从第3个位置查找(L-0,o-1,v-2)
if can't find return -1
-1
2.rindex从后向前查找,使用方法和index一样
rindex STR,SUBSTR,POSITION
rindex STR,SUBSTR
#!/usr/bin/perl use strict; my $str1="Love me, love my dog\n"; print "return the first child string location\n"; print rindex $str1,"ove"; print "\nreturn the first child string from start postition\n"; print rindex($str1,"ove",4); print "\nif can't find return -1\n"; print rindex($str1,"LOVE"); print "\n";