Chinaunix首页 | 论坛 | 博客
  • 博客访问: 838231
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-14 15:19:02

find the index of the substring in one string.
  1. $where = index($big, $small);
The index function will always report the location of the first found occurrence of the
substring.
we can use the third parameter to specify the begining index.
  1. my $stuff = "Howdy world!";
  2. my $where1 = index($stuff, "w"); # $where1 gets 2
  3. my $where2 = index($stuff, "w", $where1 + 1); # $where2 gets 6
  4. my $where3 = index($stuff, "w", $where2 + 1); # $where3 gets -1 (not found)
if the substring can’t be found at that position or later, the return value will be −1.
  1. my $last_slash = rindex("/etc/passwd", "/"); # value is 4
substr EXPR,OFFSET,LENGTH,REPLACEMENT

substr EXPR,OFFSET,LENGTH
substr EXPR,OFFSET
  1. $part = substr($string, $initial_position, $length);
It takes three arguments: a string value, a zero-based initial position (like the return
value of index), and a length for the substring. The return value is the substring:
  1. my $mineral = substr("Fred J. Flintstone", 8, 5); # gets "Flint"
  2. my $rock = substr "Fred J. Flintstone", 13, 1000; # gets "stone"
  3. my $out = substr("some very long string", −3, 2);  # $out gets "in"
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().
  1. my $s = "The black cat climbed the green tree";
  2.     my $z = substr $s, 14, 7, "jumped from"; # climbed
  3.     # $s is now "The black cat jumped from the green tree"

  1. my $string = "Hello, world!";
  2. substr($string, 0, 5) = "Goodbye";
  3. print $string;

  4. Goodbye,
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:
  1. $x = '1234';
  2.     for (substr($x,1,2)) {
  3.     $_ = 'a'; print $x,"\n"; # prints 1a4
  4.     $_ = 'xyz'; print $x,"\n"; # prints 1xyz4
  5.     $x = '56789';
  6.     $_ = 'pq'; print $x,"\n"; # prints 5pq9
  7.     }
The  sprintf function takes the same arguments as  printf (except for the optional
filehandle, of course), but it returns the requested string instead of printing it. This is
handy if you want to store a formatted string into a variable for later use, or if you want
more control over the result than printf alone would provide:

  1. my $date_tag = sprintf
  2.   "%4d/%02d/%02d %2d:%02d:%02d",
  3.   $yr, $mo, $da, $h, $m, $s;
 $date_tag gets something like "2038/01/19 3:00:08".
The leading zero
on the format number means to use leading zeros as needed to make the number as
wide as requested. Without a leading zero in the formats, the resulting date-and-time
string would have unwanted leading spaces instead of zeros, like "2038/ 1/19 3: 0: 8"

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