perl 字符串基本操作

1、Perl字符串中length取串长(字符数量)
      #!/usr/bin/Perl
      $str="abCD99e";   
      $strlen=length($str);
      print $strlen,"\n"; 

[macg@localhostPerltest]$./tip.pl
      7

2、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

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

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


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

6、用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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值