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

后端 未结 5 1133
轮回少年
轮回少年 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:05

    Substring()

    1.If start equals stop, it returns an empty string. 2.If stop is omitted, it extracts characters to the end of the string. 3.If start > stop, then substring will swap those 2 arguments. 4.If either argument is greater than the string's length, either argument will use the string's length. 5.If either argument is less than 0 or is NaN, it is treated as if it were 0.

    slice()

    1.If start equals stop, it returns an empty string, exactly like substring(). 2.If stop is omitted, slice extracts chars to the end of the string, exactly like substring(). 3.If start > stop, slice() will NOT swap the 2 arguments. 4.If either argument is greater than the string's length, either argument will use the string's length, exactly like substring().

提交回复
热议问题