perl index
来源:互联网 发布:醉仙武神武进阶数据 编辑:程序博客网 时间:2024/06/07 00:47
perl index函数的用法
2009-11-13 19:18
The
index()
function is used to determine the position of a letter or a substring in a string. For example, in the word "frog" the letter "f" is in position 0, the "r" in position 1, the "o" in 2 and the "g" in 3. The substring "ro" is in position 1. Depending on what you are trying to achieve, the
index()
function may be faster or more easy to understand than using a regular expr
split()
function.
Example 1a. Where in a string is a certain letter?
Let's say you are trying to determine if a word has a particular letter in it. Let's look for the letter "l" in the string "perlmeme.org".
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'l';
my $result = index($string, $char);
print "Result: $result\n";
This program gives you:
Result: 3
Example 1b. The string doesn't contain the letter?
If the string doesn't contain the letter,
index()
will return a -1. For example, we can look for the letter "L" in the string "perlmeme.org":
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'L';
my $result = index($string, $char);
print "Result: $result\n";
The program outputs:
Result: -1
Example 1c. The string contains more than on
If the letter we're searching for appears more than on
index()
return the index of the first occurrence of the letter.
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'e';
my $result = index($string, $char);
print "Result: $result\n";
This program gives you:
Result: 1
Looking for a substring (rather than a single character) works in exactly the same way as looking for a single character:
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $substr = 'me';
my $result = index($string, $substr);
print "Result: $result\n";
This program gives you:
Result: 4
The
index()
function let's you specify an offset. This tells
index()
where to start looking in the string. In our example, we found the first occurrence of the letter 'e' at position 1. Let's try to find the second:
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'e';
# The first 'e' was at position 1, so let's start
# looking again at position 2
my $offset = 2;
my $result = index($string, $char, $offset);
print "Result: $result\n";
The program outputs:
Result: 5
Example 3b. How to find every occurrence
To find (and do something with) every occurrence of a character in a string, you could use
index()
in a loop, incrementing the offset each time:
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'e';
my $offset = 0;
my $result = index($string, $char, $offset);
while ($result != -1) {
print "Found $char at $result\n";
$offset = $result + 1;
$result = index($string, $char, $offset);
}
When we run this program, we get the following output:
Found e at 1
Found e at 5
Found e at 7
Example 4. How to find the last occurrence
Instead of looping through every occurrence of a letter in a string to find the last on
rindex()
function. This works exactly the same as
index()
but it starts at the end of the string. The index value returned is string from the start of the string though.
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'perlmeme.org';
my $char = 'e';
my $result = rindex($string, $char);
print "Result: $result\n";
This would produce:
Result: 7
- perl index
- perl index和rindex的用法
- perl index和rindex的用法
- Index
- Index
- index
- index
- index
- index
- index
- Index
- index
- index
- index()
- Index
- index
- INDEX
- index
- 数据库范式
- IE和Firefox下javascript的兼容写法小结
- get_mid_string.c
- 搭建qt vs2005
- Linux驱动之设备模型(1) .
- perl index
- 使用反射动态调用类成员,需要Type类的一个方法:InvokeMember。对该方法的声明如下(摘抄于MSDN):
- Linux驱动之设备模型(2) .
- BMP位图浅析
- Javascript下IE与Firefox下的差异兼容写法总结
- ContentResolver 程序间数据的传递
- vmware中网卡不工作问题的解决
- bash删除文件中的空行
- 各大网站IPv6精准地址