zoukankan      html  css  js  c++  java
  • perl的一些函数(一)

      1. index 函数

              index 主要用于字符串查找,返回从左->右查到子字符串的起始位置(起始位置0) ,可以带括号,也可以不带。当找不到会返回-1

    使用方法:   

    index STR,SUBSTR,POSITION
    index STR,SUBSTR

    实例:

    
    

    #!/usr/bin/perl
    use strict;
    my $str1="Love me, love my dog\n";
    print "return the first child string location\n";
    print index $str1,"ove";
    print "\nreturn the first child string from start postition\n";
    print index($str1,"ove",2);
    print "\nif can't find return -1\n";
    print index($str1,"LOVE");
    print "\n";

     

    结果:

    D:\>perl index.pl     
    return the first child string location                            查找ove, ove 总共有出现第一次出现1,第二次10
    1                                                               
    return the first child string from start postition
    10                                                                         <-----------从第3个位置查找(L-0,o-1,v-2)
    if can't find return -1
    -1

    2.rindex从后向前查找,使用方法和index一样

      

    rindex STR,SUBSTR,POSITION
    rindex STR,SUBSTR

    #!/usr/bin/perl
    use strict;
    my $str1="Love me, love my dog\n";
    print "return the first child string location\n";
    print rindex $str1,"ove";
    print "\nreturn the first child string from start postition\n";
    print rindex($str1,"ove",4);
    print "\nif can't find return -1\n";
    print rindex($str1,"LOVE");
    print "\n";
    View Code

    运行结果:

    D:\>perl rindex.pl
    return the first child string location
    10
    return the first child string from start postition
    1
    if can't find return -1
    -1

     2.printf 函数

     printf FILEHANDLE FORMAT, LIST
     printf FILEHANDLE
     printf FORMAT, LIST

    printf 是从C语言移植过来的,参数和使用方法与C语言一样;

    %表示域说明的开始,域标识符有以下

    #!/usr/bin/perl
    use strict;
    my $float_number="10.789";
    printf("%.2f\n",$float_number);
    printf("%c\n",65);
    printf("%10s\n","this is a test,ten characters");
    printf("%-30s\n","Love perl");
    printf("%30s\n","Love perl");
    printf("%d\n",102.88);
    printf("%o\n",10);
    

      

    printf部分域说明符
    与类型 含义
    d 输出整形
    s 输出字符串
    f 输出浮点数
    c

    字符

    %.2f 保留2位小数,四舍五入

    %10 s靠右对齐,10个字符字

    结果:

    D:\>perl printf.pl
    10.79
    A
    this is a test,ten characters
    Love perl
                         Love perl
    102
    12          #          %o 将数字转换为八进制

     

    转载请注明出处:http://www.cnblogs.com/tobecrazy/

    软件测试交流QQ群:312937087 we are QA!

  • 相关阅读:
    linux 安装 jdk,Redis 安装
    cron 和 crontab -e 命令不同,crontab -e 没有秒的概念
    为什么要用 List list = new ArrayList() ,而不用 ArrayList alist = new ArrayList()呢?
    mybatis 动态sql 查询 一个参数,不要用 test = ‘id’
    乐观锁 version 悲观锁 行表锁
    Developer Test-Java
    JQuery将DIV的滚动条滚动到指定的位置
    前端学习网站
    jQuery方法大全
    JavaScript基础常用函数和语法集合大全
  • 原文地址:https://www.cnblogs.com/tobecrazy/p/3138227.html
Copyright © 2011-2022 走看看