美文网首页
Javascript学习笔记——7.12 作为数组的字符串

Javascript学习笔记——7.12 作为数组的字符串

作者: IFELSE | 来源:发表于2018-06-14 12:15 被阅读0次

    字符串除了使用charAt()方法访问单个字符外,还可以用方括号。

    var s = "hello world"
    s.charAt(3) //l
    s[3] //l
    typeof(s)
    //"string"
    Array.isArray(s)
    //false
    
    • 字符串的类数组行为使得一些数组方法可以应用到字符串上
    var s = "abcde"
    var s1 = Array.join(s,",")
    console.log(s1)
    // a,b,c,d,e
    console.log(s)
    // abcde
    s1 = Array.prototype.join.call(s,",")
    console.log(s1)
    // a,b,c,d,e
    console.log(s)
    // abcde
    s1 = Array.filter(s,function(x){return x<'d'}).join()
    console.log(s1)
    // abc
    
    • 因为字符串是只读的,所以push、sort、reverse、splice等修改原数组的方法在字符串上不适用,但不会报错。

    相关文章

      网友评论

          本文标题:Javascript学习笔记——7.12 作为数组的字符串

          本文链接:https://www.haomeiwen.com/subject/siwvsftx.html