美文网首页
JS字符串截取,字符串分割,拼接等

JS字符串截取,字符串分割,拼接等

作者: i_木木木木木 | 来源:发表于2019-08-14 12:36 被阅读0次
  • substring(start,end) start到end之间的字符串,包括start位置的字符,不包括end位置的字符(下标0开始)。
  • substr(start,num)表示从start开始的num个字符(下标0开始)。
  • slice(start,end) start到end之间的字符串,包括start位置的字符,不包括end位置的字符(下标0开始)。
   let str = 'hello world'
   console.log(str1.substring(3,7))      ////lo w
   console.log(str1.substr(3,7))      ////lo worl
   console.log(str1.slice(3,7))      ////lo w
  • split() 按照指定的分隔符把一个字符串分割存储到数组
   let arr = [];
   let str="jpg|bmp|gif|ico|png";
   arr=str.split("|");
   console.log(arr);    ////["jpg", "bmp", "gif", "ico", "png"]
  • join()将数组以指定字符拼接合并为一个字符串
  let arr = ["jpg", "bmp", "gif", "ico", "png"];
  let str = arr.join('-');
  console.log(str);    //// "jpg-bmp-gif-ico-png"
  • indexOf() 返回字符串中匹配子串的第一个字符的索引(下标0开始)
     let str = "hello world javascript!";
     let index1 =str.indexOf("java");    
     let index2 =str.indexOf("jAva");
     console.log(index1)    //12
     console.log(index2)    //-1
  • lastIndexOf()方法返回从右向左开始,出现某个字符或字符串的首个字符索引值
     let  src="hello world javascript!";
     let index1 =str.indexOf("java");    
     let index2 =str.indexOf("jAva");

     console.log(index1)    //12
     console.log(index2)    //-1

相关文章

网友评论

      本文标题:JS字符串截取,字符串分割,拼接等

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