转:perl字符串处理函数

字串的操作

這裡主要說明截取子字串、字串的長度、大小寫轉換、尋找某子字串等操作

子字串

比如:$str="mynameis"; "myn" 就是 "mynameis" 的子字串。

Perl 提供 substr 這函式,可以幫我們截取子字串。

語法:

傳回子字串 = substr 字串, 開始位置, [截取長度]

註:開始位置由 0 計數起,若開始位置之值為負整數,則由字串結尾開始計算截取位置,
比如:-1 表示最後一個字元開始截取,-3 表示倒數第三個字元開始截取。若省略了截取
長度,則表示由開始截取位置一直到字串結束。

例:

$str = "ABCDEFG1234567";

$a = substr $str, 0, 5; # 由第一個字元開始截取 5 個字元長度

print $a;

# 得:ABCDE


$a = substr $str, 3, 4; # 由第 4 個字元開始截取 4 個字元長度

print $a;

# 得:DEFG


$a = substr $str, 5; # 第6個字元開始截取至字串結束

print $a;

# 得:FG1234567


$a = substr $str, -1; # 最後一個字元

print $a;

# 得:7


$a = substr $str, -4, 2; # 最後第四個字元開始截取2個字元長度

print $a;

# 得:45

字串的長度

Perl 提供 length 函式,可用來計算字串的長度。

語法:

$str="我們一起看雲去";
$str_len = length($str);

print $str_len, "/n/n";

轉換大小寫

Perl 提供 uc / lc 函式,可轉字串為大寫/小寫。

語法:

# 轉成大寫
$str = uc(字串);

# 轉成小寫
$str = lc(字串);

$str="abCD99e";
$str = uc($str); # 此時 $str 為 ABCD99E
$str = lc($str); # 此時 $str 為 abcd99e

找尋子字串

Perl 提供 index 函式,可在字串中找尋某一子字串的起始位置。

語法:

$pos = index($str1, $str2);

# 找尋 $str2 在 $str1 中的起始位置

找尋子字串的最後位置

Perl 提供 rindex 函式,可在字串中找尋某一子字串最後的起始位置。

語法:

$pos = rindex($str1, $str2, $pos);

# 由 $str1 的 $pos 位置開始找起,找尋 $str2 在 $str1 中最後的起始位置
# 若 $pos 省略,則由字串的最後面開始找。

傳回 ASCII 值

Perl 提供 ord 函式,可傳回某一字元的 ASCII 值。

語法:

$num = ord(字元);


用例:

$num = ord('a');

print "$num/n";

chr 函式,則可將 ASCII 值轉成字元。

語法:

$char = chr(數字);


用例:

$char = chr(48);
http://linux.tnc.edu.tw/techdoc/perl_intro/x348.html

print "$char/n";

---------------------------------------
#!/usr/bin/perl


#-----------------------------

#substr用来存取子串,可以修改子串,主要用法如下:

#$value = substr($string, $offset, $count);

#$value = substr($string, $offset);


#substr($string, $offset, $count) = $newstring;

#substr($string, $offset) = $newtail;

#-----------------------------

# 首先得到一个5个字符的字符串,然后跳过三个字符,分别得到2个8字符的字符串,最后剩下的给$trailing

#unpack/pack的用法以后会讲到,或者参见google 一下 ‘perl函数 unpack’

($leading, $s1, $s2, $trailing) =
    unpack("A5 x3 A8 A8 A*", $data);

# 将字符串分成每5个字符一个串,存入数组@fives

@fivers = unpack("A5" x (length($string)/5), $string);

# 将字符串打散成单个字符,存入数组@chars

@chars = unpack("A1" x length($string), $string);
#-----------------------------

$string = "This is what you have";
# +012345678901234567890 Indexing forwards (left to right)

# 109876543210987654321- Indexing backwards (right to left)

# note that 0 means 10 or 20, etc. above

#下面是一些例子:

$first = substr($string, 0, 1);
# "T"

$start = substr($string, 5, 2);
# "is"

$rest = substr($string, 13);
# "you have"

$last = substr($string, -1);
# "e"

$end = substr($string, -4);
# "have"

$piece = substr($string, -8, 3);
# "you"

#-----------------------------

$string = "This is what you have";
print $string;
#This is what you have


substr($string, 5, 2) = "wasn't";
# 改变 "is" 为 "wasn't"

#This wasn't what you have


substr($string, -12) = "ondrous";
# 替换最后12个字符

#This wasn't wondrous


substr($string, 0, 1) = "";
# 删除首字符

#his wasn't wondrous


substr($string, -10) = "";
# 删除最后10个字符

#his wasn'

#-----------------------------

# 你可以用 =~ 来测试子串,=~为正则表达式匹配运算符,后面会讲到,还可以google Perl 正则表达式

#主要是匹配则为True;否则为False。 pattern可以自己根据需要构造。

if (substr($string, -10) =~ /pattern/) {
    print "Pattern matches in last 10 characters/n";
}

# 将 "is" 换为 "at", 限制在最后五个字符;=~ s/// 为替换表达式。

substr($string, 0, 5) =~ s/is/at/g;
#-----------------------------

# 将字符串$a的第一个和最后一个字符交换

$a = "make a hat";
(substr($a,0,1), substr($a,-1)) = (substr($a,-1), substr($a,0,1));
print $a;
# take a ham

#-----------------------------

# 抽取某些子串

$a = "To be or not to be";
$b = unpack("x6 A6", $a);
# 跳过6个字符,抽取6个字符

print $b;
# or not


($b, $c) = unpack("x6 A2 X5 A2", $a);
# 跳过6个字符, 抽出2个字符的子串;后退5个字符,抽出2个字符的子串

print "$b/n$c/n";
# or

#

# be

#-----------------------------

#下面是一个综合的例子,主要是一个函数,实现了

#一种模板格式的输出。

sub cut2fmt {
    my(@positions) = @_;
    my $template = '';
    my $lastpos = 1;
    foreach $place (@positions) {
        $template .= "A" . ($place - $lastpos) . " ";
        $lastpos = $place;
    }
    $template .= "A*";
    return $template;
}

$fmt = cut2fmt(8, 14, 20, 26, 30);
print "$fmt/n";
# A7 A6 A6 A6 A4 A*

#-----------------------------
http://blog.chinaunix.net/u/20754/showart_223337.html
---------------------------------------------
perl字符串处理函数
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,lc,uc,lcfirst,ucfirst
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
http://blog.chinaunix.net/u/25264/showart.php?id=1412747
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值