字符串

instanceof & typeof:

<script>
    var s="hello";
    var i=8;
// typeof 只能判断基本数据类型;
    alert(typeof (s)); //string
    alert(typeof (i)); //number

    var s2=new String("hello2");
    alert(typeof(s2)); //object
    alert( s2 instanceof String); //True

    var n=new Number(2);
    alert(typeof (n));  //object
    alert( n instanceof String); //False

</script>

 字符串的相关方法

 String对象的属性 length
    alert(s.length);

 遍历字符串:
    for (var i in s){console.log(s[i])};

-------------------------String方法---------
    编排方法:
       document.write(s.italics());
       document.write(s.bold());
       document.write(s.anchor("alex"));

    大小写转换:

        console.log(s.toUpperCase());//全部大写
        console.log(s.toLowerCase()); //全部小写

    获取指定字符:
        console.log(s.charAt(3)); //字符串制定位置上的字符
        console.log(s.charCodeAt(3)); //ASCII 字符编码字符数字 76

     查询字符串match();  search():

       console.log(s.search("E"));  //返回的第一个匹配结果的索引值
       console.log(s.match("E")[0]);   // 返回数组,里面是所有匹配结果
       console.log(s.match("E")[1]);   // 返回数组,里面是所有匹配结果,没有则返回undefined

       console.log(s.indexOf("E"));  // 从左向右取第一个"E"的索引  (第一个)
       console.log(s.lastIndexOf("E"));  // 从右向左取第一个"E"的索引 (最后一个)


     replace concat split

       console.log(s.replace("E","e"));// 字符串替换,前面的替换为后面的
       console.log(s.split("E")); //字符串以"E"分割
       console.log(s.concat(" world")) //字符串拼接

     截取字符串
        console.log(s.substr(1,1));   //索引位置,长度(长度包含索引位置)
        console.log(s.substring(1,4)); //两个索引位置 (不包含后索引位置上的元素,左含右不含)
        console.log(s.slice(1,-1));  //后索引,倒数第几个,不包含
原文地址:https://www.cnblogs.com/jiefangzhe/p/8095582.html