substr summary

substr总结:
substr EXPR,OFFSET,LENGTH,REPLACEMENT 
substr EXPR,OFFSET,LENGTH 
substr EXPR,OFFSET 
1、从一个字符串中提取一个子字符串并返回它。
OFFSET是起始位置,为正时从前面开始数,为负时从后面开始数,数的方向都是从起始位置向后面数。省略时默认值为0。
LENGTH 是向后数的长度,直接向后数length个字符,超出字符串长度时直接数到字符串结尾。为负时,从offset数到从字符串结尾向前数的length后结束。省略时默认为数到结尾。
my $s = "The black cat climbed the green tree";
my $color = substr $s, 4, 5;      # black  从第4个字符开始向后数5个。
my $middle = substr $s, 4, -11;
# black cat climbed the从第4个字符开始数到倒数第11个字符结束。不包含倒数第11个字符。
my $end = substr $s, 14;        # climbed the green tree 从第14个字符数到结束。
my $tail = substr $s, -4;        # tree 从倒数第4个字符数到结束。
my $z = substr $s, -4, 2;     # tr 从倒数第4个字符向后数2个字符。
2、substr()函数本身可以作为左值。如果指定的长度比length短,string将会变短,反之变长,这里相当于替换,用右值替换substr指定的字符串。为了保证相同长度,你可以用sprintf来赋值。
用offset和length指定的字串部分超出母串,只返回为超出母串的部分,不会出现警告。如果初始位置超出母串,那么会出现一个警告,返回未定义的值,同时会出现警告,这是作为左值时,会出现异常。下面是这几种情况的例子:
my $name = 'fred';
substr($name, 4) = 'dy';         # $name is now 'freddy'
my $null = substr $name, 6, 2;   # returns "" (no warning)
my $oops = substr $name, 7;      # returns undef, with warning
substr($name, 7) = 'gap';        # raises an exception
3、用substr的第4个参数可以用作替换操作,替换母串中部分字符然后返回替换后的字串,功能和splice()类似。
my $s = "The black cat climbed the green tree";
my $z = substr $s, 14, 7, "jumped from";    # climbed
# $s is now "The black cat jumped from the green tree"
4、由前三个参数组成的substr()函数作为左值时,可以用作“magic bullet”,每次被执行,它都会使用最新的字符串,例如:
$x = '1234';
for (substr($x,1,2)) {
$_ = 'a';   print $x,"\n";    # prints 1a4
$_ = 'xyz'; print $x,"\n";    # prints 1xyz4
$x = '56789';
$_ = 'pq';  print $x,"\n";    # prints 5pq9
}
此时相当于:
my $x = '1234';
{
substr($x,1,2) = 'a'; print $x,"\n";
substr($x,1,2) = 'xyz'; print $x,"\n";
$x = '56789';
substr($x,1,2) = 'pq';  print $x,"\n";
}
 
Extracts a substring out of EXPR and returns it. First character is at offset 0 (or whatever you've set $[ to (but <don't do that)). If OFFSET is negative (or more precisely, less than $[ ), starts that far back from the end of the string. If LENGTH is omitted, returns everything through the end of the string. If LENGTH is negative, leaves that many characters off the end of the string.
You can use the substr() function as an lvalue, in which case EXPR must itself be an lvalue. If you assign something shorter than LENGTH, the string will shrink, and if you assign something longer than LENGTH, the string will grow to accommodate it. To keep the string the same length, you may need to pad or chop your value using sprintf.
If OFFSET and LENGTH specify a substring that is partly outside the string, only the part within the string is returned. If the substring is beyond either end of the string, substr() returns the undefined value and produces a warning. When used as an lvalue, specifying a substring that is entirely outside the string raises an exception. Here's an example showing the behavior for boundary cases:
An alternative to using substr() as an lvalue is to specify the replacement string as the 4th argument. This allows you to replace parts of the EXPR and return what was there before in one operation, just as you can with splice().
Note that the lvalue returned by the three-argument version of substr() acts as a 'magic bullet'; each time it is assigned to, it remembers which part of the original string is being modified; for example:
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值