#!/usr/bin/Perl
$str="abCD99e";
$strlen=length($str);
print $strlen,"\n";
[macg@localhostPerltest]$./tip.pl
7
2、substr串,位置,长度-------取子串,注意从0开始数位置
#!/usr/bin/Perl
$str="ABCDEFG1234567";
$a=substr$str,0,5;
print $a,"\n";
[macg@localhostPerltest]$./tip.pl
ABCDE
$a=substr$str,-4,2;
从倒数第4个开始,取两个字符
[macg@localhostPerltest]$./tip.pl
45
3、index在字串中找尋某一子字串的起始位置
#!/usr/bin/Perl
$str="ABCDEFG1234567";
$a="12";
$pos=index($str,$a);
print $pos,"\n";
[macg@localhostPerltest]$./tip.pl
7
4、@数组=split(pattern,串)将Perl字符串用某模式分成多个单词
#!/usr/bin/Perl
$str="ABCDEiFG12i34567";
@array=split(//,$str);按空格分
foreach(@array){
print $_,"\n";
}
[macg@localhostPerltest]$./tip.pl
ABCDEi
FG12i
345
6
7
@array=split(/+/,$line);当一行中各单词间的空格多于一个时
5、空格和TAB混杂情况下的split
[macg@localhostPerltest]$vitip.pl
#!/usr/bin/Perl
$str="ABCDEiFG12i34567";
@array=split(/\t/,$str);
foreach(@array){
print $_,"\n";
}
[macg@localhostPerltest]$./tip.pl
ABCDEiFG12i
34567
只分了两份,为什么?
因为同时满足TAB和空格的只有一处
所以必须加[]
@array=split(/[\t]/,$str);现在才是真正的按空格和TAB分
[macg@localhostPerltest]$./tip.pl
ABCDEi
FG12i
345
6
7
但还是有缺陷,TAB和空格相连时,TAB被认为是空格划分的子串,或者空格被认为是TAB划分的子串
6、用join定义Perl字符串数组格式符号(缺省是,)必须与qw()合用
语法:join($string,@array)
@array=qw(onetwothree);
$total="one,two,three";
@array=qw(onetwothree);
$total=join(":",@array);
$total="one:two:three";
数组内grep
@array=("one","on","in");
$count=grep(/on/,@array);
查询结果赋值给单变量
@array=("one","on","in");
@result=grep(/on/,@array);
查询结果赋值给数组
2
one
on