Perl字符串基本操作详解

开发 架构
本文向大家介绍一下Perl字符串的一些操作,Perl字符串在Perl语言中经常会用的,掌握它的一些基本操作对你的学习和使用都有好处。

本文和大家重点讨论一下Perl字符串的一些基本操作,比如Perl字符串数组元素赋值:@tmp=qw(aaabbbkkk9000);相当于@tmp=(“aaa”,“bbb”,“kkk”,“9000)。至于其他操作请看本文详细介绍。

Perl字符串操作

Perl字符串数组元素赋值:@tmp=qw(aaabbbkkk9000);相当于@tmp=(“aaa”,“bbb”,“kkk”,“9000);

◆Perl字符串比较,绝不能用==,要用eq
[macg@localhostPerltest]$vitip.pl

#!/usr/bin/Perl
print"input:";
while(chomp($input=<>)){
print"yourinputis$input\n";
if($input=="q"){print"chooseq\n";last;}
elsif($input=='n'){print"inputis$input\n";next;}
else{print"inputok,tryagain\n";}
print"input:";
}
[macg@localhostPerltest]$./tip.pl
input:x
yourinputisx
chooseq


◆Perl字符串用==是最常犯的错误

即使是整形,也尽量用eq,少用==
while(chomp($input=<STDIN>))
{
for($i=1,$found=0;$i<=$int_num;$i++)
{
if($input==$i){$found=1;}
else
Doyouwanttochangeeth0:2'sipaddress?回车

Argument""isn'tnumericinnumericeq(==)at./address.plline77,<STDIN>line2.
对整形变量$input==$i,如果$input是回车,并不走else,而是报错

正确的做法是:不论整形Perl字符串,都用eq
while(chomp($input=<STDIN>))
{
for($i=1,$found=0;$i<=$int_num;$i++)
{
if($inputeq$i){$found=1;}
}
whichinterfaceyouwanttoconfig?choiceanumber1234q:1
Doyouwanttochangeeth0'sipaddress?


◆Perl字符串几种连接运算符

运算符,常用于输出
print"純金",$v1;
print$str,"\n\n";

.运算符和,类似也是Perl字符串相加但,通常只用于print而.可以用在任何Perl字符串相加的地方
print'12345大家來跳舞'."helloworld";
結果變成:
12345大家來跳舞helloworld

x运算符号
print"OK"x4;
結果變成:
OKOKOKOK


◆为什么Perl字符串相加只能用.不能用+

因为可能+就是真加了(数字相加),而不是Perl字符串合并
$v1=99;
$v2='121';

print$v1+$v2;
$v1=99;
$v2='121';

print$v2.$v1;
220
12199

◆Perl字符串的连接可以连接整形和字符形,整形也被当作字符型处理,没有printf里的%d问题
$min=1;

$date="date"."0".$min;
print$date,"\n";

[root@ntrackermac]#./tip.pl
date01


uc轉成大寫,lc轉成小寫
$str="abCD99e";
$str=uc($str);
$str="abCD99e";
$str=lc($str);
[macg@localhostPerltest]$./tip.pl
ABCD99E
[macg@localhostPerltest]$./tip.pl
abcd99e                    #p#


◆Perl字符串中length取串长(字符数量)
#!/usr/bin/Perl
$str="abCD99e";
$strlen=length($str);
print$strlen,"\n";
[macg@localhostPerltest]$./tip.pl
7

◆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


◆index在字串中找尋某一子字串的起始位置
#!/usr/bin/Perl
$str="ABCDEFG1234567";
$a="12";
$pos=index($str,$a);
print$pos,"\n";
[macg@localhostPerltest]$./tip.pl
7

@数组=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);当一行中各单词间的空格多于一个时


◆空格和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划分的子串


◆用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
 

【编辑推荐】

  1. 解析Perl字符串用法
  2. Perl多线程的两种实现方式
  3. Perl二维数组用法全程剖析
  4. Perl基础 Perl 哈希表概述
  5. 解析Perl正则表达式中的模式

 

 

责任编辑:佚名 来源: csdn.net
相关推荐

2010-07-14 16:21:48

Perl

2010-07-14 12:39:30

Prel字符串

2010-07-14 16:35:52

Perl字符串处理函数

2010-07-14 16:48:02

Perl字符串比较

2010-07-19 15:07:46

Perl字符串处理函数

2011-08-10 18:47:18

Cocoa字符串

2015-08-14 09:37:44

Java字符串基本运算

2010-07-14 16:10:37

Perl

2010-07-14 17:00:34

Perl字符串

2010-07-15 14:01:10

Perl目录句柄

2015-06-09 14:43:36

javascript操作字符串

2009-08-24 13:04:44

操作步骤C#字符串

2022-05-26 09:31:20

Java字符串

2010-02-01 16:22:36

Python字符串操作

2021-03-11 10:00:32

Java字符串开发

2009-11-27 10:24:25

PHP字符串操作

2019-12-25 15:41:50

JavaScript程序员编程语言

2010-09-06 17:30:46

SQL函数

2009-07-15 17:20:45

Jython字符串

2021-09-10 08:18:31

Go语言字符串
点赞
收藏

51CTO技术栈公众号