perl 字符串基本操作
来源:互联网 发布:新三板数据库 编辑:程序博客网 时间:2024/05/26 12:59
#!/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
- perl 字符串基本操作
- Perl字符串基本操作详解
- Perl字符串基本操作详解
- perl 基本操作
- perl 字符串操作
- Perl的字符串操作
- perl 字符串操作
- perl 字符串相关操作
- perl字符串操作符
- 关于Perl几个基本操作的Demo
- Perl 字符串操作 以及 自定义排序
- Bash基本字符串操作
- 字符串基本操作
- 字符串基本操作
- 字符串的基本操作
- 字符串基本操作
- 字符串基本操作
- 字符串的基本操作
- 杭电水题之1201
- 设计一个递归算法,删除不带头结点的单链表L中所有值为x的结点
- 快速排序,void Qsort(void *base,int nelem,int width, int (*fcmp)(const void *,const void *))的实现。
- Index-Organized Tables
- 求pagerank算法的实验数据
- perl 字符串基本操作
- HTTP协议返回状态码大全
- newlisp获取并计算内存负载
- R-write.table族存出数据多一列
- C++ 实验二
- dyld: Library not loaded 动态库不能被加载
- 关于数组的几点总结
- 成果展示
- Log4Net保存错误日志到SQLServer的步骤与配置