JavaScript中的slice()和substr()有什么区别? [英] What is the difference between slice() and substr() in JavaScript?
本文介绍了JavaScript中的slice()和substr()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请问JavaScript中的字符串对象 slice()
和 substr()
之间有什么区别?
Can I ask what the difference is between string object slice()
and substr()
in JavaScript?
推荐答案
他们有不同的签名, .slice()
是:
They have different signatures, .slice()
is:
string.slice(beginIndex, endIndex)
而 .substr()
is:
Whereas .substr()
is:
string.substr(beginIndex, length);
例如,如果我们有1234
并且想要23
,它将是:
So for example, if we have "1234"
and wanted "23"
, it would be:
"1234".slice(1,3)
//or...
"1234".substr(1,2)
对于较少使用的否定索引,它们也有不同的行为,请查看 .slice()
和 .substr()
完整描述。
They also have different behavior for the more-rarely used negative indexes, look at the MDC documentation for .slice()
and .substr()
for full descriptions.
这篇关于JavaScript中的slice()和substr()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文