What is the difference between slice() and substr() in JavaScript?

后端 未结 5 1130
轮回少年
轮回少年 2020-12-31 04:34

Can I ask what the difference is between string object slice() and substr() in JavaScript?

5条回答
  •  灰色年华
    2020-12-31 05:11

    1. String.slice(begin, end)

      This method will cut text from begin to end char, eg.:

      alert("Hello World!".slice(1, 8)); // ello Wo
      
    2. String.substr(begin, length)

      This method will cut text from begin to begin + length char, eg.:

      alert("Hello World!".substr(1, 8)); // ello Wor
      

提交回复
热议问题