zoukankan      html  css  js  c++  java
  • js 字符串截取函数substr,substring,slice之间的差异

    js 字符串的截取,主要有三个函数,一般使用三个函数:substr,substring,slice。

    而这三个函数是不完全一样的,平时很难记住,在这里做下笔记,下次遇到的时候,直接从这里参考,调用合适的方法;

    从参数方面考量:当有一个参数(正数)的时候,这三个函数基本上没有基本上没有什么区别

    1. 有一个参数(正数):

    1  <script>
    2         var str = "abcdefghijklmnopqrstuvwxyz";
    3         var a = str.slice(2);
    4         var b = str.substr(2);
    5         var c = str.substring(2);
    6         console.log("a的值为: " + a);
    7         console.log("b的值为: " + b);
    8         console.log("c的值为: " + c);
    9     </script>

    运行结果:

    1.1 当有一个参数参数为负数时,slice() 会把 参数的值变为 : str.length + 参数 ;substr()会把参数的值变为:str.length + 参数 ;substring() 会把参数直接变为 :0

    1 <script>
    2         var str = "abcdefghijklmnopqrstuvwxyz";
    3         var a = str.slice(-5);
    4         var b = str.substr(-5);
    5         var c = str.substring(-5);
    6         console.log("a的值为: " + a);
    7         console.log("b的值为: " + b);
    8         console.log("c的值为: " + c);
    9     </script>

    运行结果:

    2.当有两个参数(都为正数)时,slice()的第一个参数为起始位置,第二个参数为结束位置(但是不包括结束位);substr()的第一个参数为起始位置,第二个参数为 要截取的字符串长度;substring()第一个参数为起始位置,第二个参数为结束位置(但是不包括结束位)

    1  <script>
    2         var str = "abcdefghijklmnopqrstuvwxyz";
    3         var a = str.slice(3, 10);
    4         var b = str.substr(3, 10);
    5         var c = str.substring(3, 10);
    6         console.log("a的值为: " + a);
    7         console.log("b的值为: " + b);
    8         console.log("c的值为: " + c);
    9     </script>

    运行结果:

    2.1 当有两个参数(第一个为负,第二个为正),slice() 会把第一个参数变为:str.length+参数;substr()会把第一个参数变为:str.length + 参数;substring()会把参数变为: 0

    1 <script>
    2         var str = "abcdefghijklmnopqrstuvwxyz";
    3         var a = str.slice(-3, 10);
    4         var b = str.substr(-3, 10);
    5         var c = str.substring(-3, 10);
    6         console.log("a的值为: " + a);
    7         console.log("b的值为: " + b);
    8         console.log("c的值为: " + c);
    9     </script>

    运行结果:

    解析如下:

    2.2 当有两个参数(第一个为正,第二个为负),slice()会把第二个参数变为:str.length + 参数;substr()会把第二个参数变为:0 ;substring()会把第二个参数变为0,然后对比连个参数的大小,小的放前面

    1   <script>
    2         var str = "abcdefghijklmnopqrstuvwxyz";
    3         var a = str.slice(10, -3);
    4         var b = str.substr(10, -3);
    5         var c = str.substring(10, -3);
    6         console.log("a的值为: " + a);
    7         console.log("b的值为: " + b);
    8         console.log("c的值为: " + c);
    9     </script>

    运行结果:

    解析:

    2.3 当有两个参数(第一个为负,第二个为负)。slice() 函数会把两个参数都变为:str.length + 参数 ;substr()函数会把第一个参数变为:str.length + 参数,把第二个参数变为:0;substring()函数会把两个参数都变为:0

    1 <script>
    2         var str = "abcdefghijklmnopqrstuvwxyz";
    3         var a = str.slice(-10, -3);
    4         var b = str.substr(-10, -3);
    5         var c = str.substring(-10, -3);
    6         console.log("a的值为: " + a);
    7         console.log("b的值为: " + b);
    8         console.log("c的值为: " + c);
    9     </script>

    运行结果:

    解析:

  • 相关阅读:
    双向链表的创建,查找,删除
    atoi 函数自实现
    strcmp,strcpy,strcat,strncmp,strncpy,strncat,自实现精炼版本
    天生棋局(堆上申请二维空间的应用)
    使用二级指针,初始化一级指针
    指针数组的简单理解
    输入二进制数,输出10进制数
    阶乘循环联系题
    [剑指offer] 二叉搜索树的第k个节点
    二叉树的序列化和反序列化
  • 原文地址:https://www.cnblogs.com/huanying2015/p/10485393.html
Copyright © 2011-2022 走看看