Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1148008
  • 博文数量: 312
  • 博客积分: 12522
  • 博客等级: 上将
  • 技术积分: 3376
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-27 18:35
文章分类

全部博文(312)

文章存档

2016年(3)

2015年(1)

2013年(1)

2012年(28)

2011年(101)

2010年(72)

2009年(13)

2008年(93)

分类: Python/Ruby

2011-12-08 17:11:45

1. index

position = index (string, substring, position)

返回子串substring在字符串string中的位置,如果不存在则返回-1。参数position是可选项,表示匹配之前跳过的字符数,或者说从该位置开始匹配。

 

例子如下:

[root@localhost ~]# echo -n '/var/fap/test/123' | perl -ne '$rev=index($_, "a"); print $rev,"\n";'

2

[root@localhost ~]# echo -n '/var/fap/test/123' | perl -ne '$rev=index($_, "a", 1); print $rev,"\n";'

2

[root@localhost ~]# echo -n '/var/fap/test/123' | perl -ne '$rev=index($_, "a", 3); print $rev,"\n";'

6

[root@localhost ~]# echo -n '/var/fap/test/123' | perl -ne '$rev=index($_, "a", 7); print $rev,"\n";'

-1

 

 

2. rindex

position = rindex (string, substring, position)

index类似,区别是从右端匹配。

 

例子如下:

 

[root@localhost ~]# echo -n '/var/ftp/tesa/123' | perl -ne '$rev=rindex($_, "a", 11); print $rev,"\n";'

2

[root@localhost ~]# echo -n '/var/ftp/tesa/123' | perl -ne '$rev=rindex($_, "a", 12); print $rev,"\n";'

12

[root@localhost ~]# echo -n '/var/ftp/tesa/123' | perl -ne '$rev=rindex($_, "a", 1); print $rev,"\n";'

-1

[root@localhost ~]# echo -n '/var/ftp/tesa/123' | perl -ne '$rev=rindex($_, "a", 2); print $rev,"\n";'

2

 

 

3. length

num = length (string)

返回字符串长度,或者说含有字符的数目。

 

例子如下:

 

[root@localhost ~]# echo -n '/var/ftp/tesa/123' | perl -ne '$rev=length($_); print $rev,"\n";'

17

[root@localhost ~]# echo -n '/var/ftp/tesa/123  ' | perl -ne '$rev=length($_); print $rev,"\n";'

19

 

4. substr

substr (expr, skipchars, length)

抽取字符串(或表达式生成的字符串)expr中的子串,跳过skipchars个字符,或者说从位置skipchars开始抽取子串(第一个字符位置为0),子串长度为length

 

此参数可忽略,意味着取剩下的全部字符。

当此函数出现在等式左边时,expr必须为变量或数组元素,此时其中部分子串被等式右边的值替换。

 

substr() 函数的作用有两个:替换一部分子串。 删除一部分子串。

 

例子如下:

 

[root@localhost ~]# echo -n '/var/ftp/test/123' | perl -ne '$rev=substr($_, 9,);print $rev,"\n";'

test/123

[root@localhost ~]# echo -n '/var/ftp/test/123' | perl -ne '$rev=substr($_, 9, 4);print $rev,"\n";'

test

 

替换:

[root@localhost ~]# echo -n '/var/ftp/test/123' | perl -ne '$rev=substr($_, 9, 4)="hello"; print $rev,"\n";'

hello

删除:

[root@localhost ~]# echo -n '/var/ftp/test/123' | perl -ne '$rev=substr($_, 9, 4)=""; print $rev,"\n";'

 

 

5. lcuclcfirstucfirst

lc,将字符串改为小写

uc,将字符串改为大写

lcfirst,改变字符串首字母小写

ucfirst,改变字符串首字母大写

 

例子如下:

[root@localhost ~]# echo -n 'hello, hanli' | perl -ne '$rev=uc($_); print $rev,"\n";'

HELLO, HANLI

[root@localhost ~]# echo -n 'HELLO, hanli' | perl -ne '$rev=lc($_); print $rev,"\n";'

hello, hanli

[root@localhost ~]# echo -n 'hello, Hanli' | perl -ne '$rev=ucfirst($_); print $rev,"\n";'

Hello, Hanli

[root@localhost ~]# echo -n 'hello, Hanli' | perl -ne '$rev=lcfirst($_); print $rev,"\n";'

hello, Hanli

 

Perl字符串处理函数大全

 

Perl字符串处理函数的概念你是否了解,本文向大家介绍几种Perl字符串处理函数用法,希望通过本文的介绍你对Perl字符串处理函数有初步的认识。

AD:

本文和大家重点讨论一下Perl字符串处理函数的用法,每种函数都有各自的特点和作用,下面请看本文详细介绍,相信通过本文的学习你对Perl字符串处理函数一定会有所了解。

Perl字符串处理函数

Perl字符串处理函数index

调用语法position=index(string,substring,position);
解说返回子串substring在字符串string中的位置,如果不存在则返回-1。参数position是可选项,表示匹配之前跳过的字符数,或者说从该位置开始匹配。

Perl字符串处理函数rindex

调用语法position=rindex(string,substring,position);
解说与index类似,区别是从右端匹配。

Perl字符串处理函数length

调用语法num=length(string);
解说返回字符串长度,或者说含有字符的数目。

Perl字符串处理函数pos

调用语法offset=pos(string);
解说返回最后一次模式匹配的位置。

Perl字符串处理函数substr

调用语法substr(expr,skipchars,length)
解说抽取字符串(或表达式生成的字符串)expr中的子串,跳过skipchars个字符,或者说从位置skipchars开始抽取子串(第一个字符位置为0),子串长度为length,此参数可忽略,意味着取剩下的全部字符。
当此函数出现在等式左边时,expr必须为变量或数组元素,此时其中部分子串被等式右边的值替换。

Perl字符串处理函数study

调用语法study(scalar);
解说用一种内部格式提高变量的访问速度,同一时刻只对一个变量起作用。

Perl字符串处理函数lc
uc
调用语法retval=lc(string);
retval=uc(string);
解说将字符串全部转换成小/大写字母。

Perl字符串处理函数lcfirst

ucfirst
调用语法retval=lcfirst(string);
retval=ucfirst(string);
解说将第一个字母转换成小/大写。

Perl字符串处理函数quotameta

调用语法newstring=quotemeta(oldstring);
解说将非单词的字母前面加上反斜线(\)。
语句:$string=quotemeta($string);
等效于:$string=~s/(\W)/\\$1/g;
常用于模式匹配操作中,确保字符串中没有字符被看作匹配操作符。

Perl字符串处理函数join

调用语法join(joinstr,list);
解说把字符串列表(数组)组合成一个长的字符串,在每两个列表元素间插入串joinstr。

Perl字符串处理函数sprintf

调用语法sprintf(string,fields);
解说与printf类似,区别是结果不输出到文件,而作为返回值赋给变量。
例子$num=26;
$outstr=sprintf("%d=%xhexadecimalor%ooctal\n",$num,$num,$num);
print($outstr);
结果输出26=1ahexadecimalor32octal

阅读(1004) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~