js字符串方法:截取、查找、转化为数组

      //查找字符串
         //1.chartAt()返回指定位置的字符例如第0位是哪个字母
         var str = "hello world";
         console.log(str.charAt(0));
         //indexOf()检索字符串例如h在哪个位置,第一次出现的位置,没有返回-1
         console.log(str.indexOf("h"));
         //3.charCodeAt() unicode编码
         console.log(str.charCodeAt(0));//104
         //截取字符串
         //1.slice(start,end)提取字符串片段,并在新的字符串中返回被截取的部分
         var str = "hello world"
         console.log(str.slice(6));//world包括开始位置的剩下所有字符串空格也算一位
         console.log(str.slice(0,5));//hello包括开始,不包括结束的所有字符
         //2.substr(start,length)从起始索引号提取字符串中指定数目字符
         console.log(str.substr(3));//lo word 包括开始位置剩下的所有字符
         console.log(str.substr(3,4));//lo w 从第三位开始截取4个字符,空格也算一个字符
         //3.substring()提取两个索引号之间的字符,不接受负值
         console.log(str.substring(3))//lo word
         console.log(str.substring(3,4))//1 从第三位字符开始到第四位字符结束中间的所有字符,包括开始,不包括结束
         //转化为数组
         //splice()把字符串分割成字符串数组
         console.log(str.split(" "));//["hello","world"] 通过空格将字符串分割成数组

 

转载于:https://www.cnblogs.com/xiyuyizhihua/p/10901336.html

你可能感兴趣的:(js字符串方法:截取、查找、转化为数组)