• 数组、字符串对象的各种方法


    数组的常用方法

    1,shift()方法:把数组的第一个元素删除,并返回第一个元素的值

    var a = ['a', 'b', 'c'];
    console.log(a,a.shift());
    //['b','c']     'a'

    2,pop():用于删除并返回数组的最后一个(删除元素)元素,如果数组为空则返回undefined ,把数组长度减 1

    var a = ['a', 'b', 'c'];
    console.log(a,a.pop());
    //["a", "b"]      "c"

    3,unshift() :将参数添加到原数组开头,并返回数组的长度 

    var movePos =[111,222,333,444];
    movePos.unshift("55555")
    document.write(movePos)     //["55555",111,222,333,444]

    4,push():可向数组的末尾添加一个或多个元素,并返回新的长度,(用来改变数组长度)。

    var movePos=[11,22];
    
    var arr=movePos.push("333");
    
    console.log(movePos,arr) //[11, 22, "333"] 3

    5,concat()方法:用于连接两个或多个数组,并返回一个新数组,新数组是将参数添加到原数组中构成的 

    var movePos=[11,22];
    var arr=movePos.concat(4,5);
    console.log(arr);//[11, 22, 4, 5]

    6,join()方法:用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的。

    var movePos=[11,22];
    var arr=movePos.join("+");
    console.log(arr)  //11+22

    7,slice()方法:可从已有的数组中返回选定的元素。slice(开始截取位置,结束截取位置)

    var movePos=[11,22,33];
    var arr=movePos.slice(1,2);
    console.log(arr)//[22]

    8,splice()方法:方法向/从数组中添加/删除项目,然后返回被删除的项目。

    var movePos=[11,22,33,44];
    var arr=movePos.splice(1,2);//删除
    console.log(arr,movePos)
     [22, 33]     [11, 44]

      var movePos =[111,222,333,444];
      movePos.splice(2,1,"666")
      console.log(movePos)

      [111, 222, "666", 444]

    --------------------

    1 split:方法用于把一个字符串分割成字符串数组。

    var host="?name=232&key=23";
    host=host.split("?")
    console.log(host)
    ["", "name=232&key=23"]

    2 substring() 方法用于提取字符串中介于两个指定下标之间的字符。

    var host="?name=232&key=23";
    host=host.substring(1)
    console.log(host)
    //name=232&key=23

    var str = "hello world!";
    var str1 = "wo";

    3 indexOf() 方法:用于返回某个指定的字符串值在字符串中首次出现的位置。

      alert(str.indexOf(str1))  // 6

    4 charAt() 方法:用于返回指定位置的字符。

      alert(str.charAt(0))   // h

    5 slice() 方法:提取字符串的某个部分,并以新的字符串返回被提取的部分。

      alert(str.slice(2,5))  // llo。返回索引2-5但不包含5之间的字符

      alert(str.slice(2,-5)) // llo w。返回从索引2开始至倒数第五个位置但不包含其本身之间的字符。

      alert(str.slice(-5,-1)) // orld。返回倒数第五个位置至倒数第一个位置(不包含倒数第一个位置)之间的字符,且两个参数均为负数时,第二个参数必须大于第一个参数

    6 substr()方法:可在字符串中抽取从 start 下标开始的指定数目的字符。

      alert(str.substr(2,5))  // llo w。从索引2开始,返回5个字符

      alert(str.substr(-2,5))  // d!。即从倒数第二个字符开始返回5个字符,第一个参数可为负值,但第二个参数不可为负值

  • 相关阅读:
    XML 树操作 语法
    重构:第一个案例
    敏捷开发 慨叙
    sql server 字段类型
    Linux Win7双系统安装
    IE6/7不读取CSS样式,或不能正常显示背景图片问题
    eclipse 添加字体
    eclipse中测试Hibernate异常报 ORA00926: 缺失 VALUES 关键字
    关于简历的想法几点
    eclipse中测试Hibernate异常报'hibernate.dialect' must be set when no Connection avalable
  • 原文地址:https://www.cnblogs.com/sunjuncoder/p/9896235.html
Copyright © 2020-2023  润新知