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

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-12 17:13:34

 split, which breaks up a string ac-cording to a pattern.
@fields = split /separator/, $string;
The default for split is to break up $_ on whitespace:
my @fields = split;  # like split /\s+/, $_;

@fields = split /:/, "abc:def:g:h";  # gives ("abc", "def", "g", "h")
You could even have an empty field, if there were two delimiters together:
@fields = split /:/, "abc:def::g:h";  # gives ("abc", "def", "", "g", "h")
Here’s a rule that seems odd at first, but it rarely causes problems: leading empty fields
are always returned, but trailing empty fields are discarded. For example:‖
@fields = split /:/, ":::a:b:c:::";  # gives ("", "", "", "a", "b", "c")


A "split" on "/\s+/" is like a "split(' ')" except that any leading
               whitespace produces a null first field.  A "split" with no
               arguments really does a "split(' ', $_)" internally.

  1. $str = " 10 30 st mmmmm ";

  2. @f = split /\s+/, $str;
  3. print "@f\n";

  4. @s = split(' ', $str);
  5. @d = split(' ', $str);
  6. print "@s\n";
  7. print "@d\n";

  8. # 10 30 st mmmmm
  9. #10 30 st mmmmm
  10. #10 30 st mmmmm



join glues together a bunch of pieces to make a single string. The join function looks like this:
my $result = join $glue, @pieces;

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