更新时间:2022-06-22 02:42:09
slice( )
的作用类似于 substring()
,但有一些不同的行为。
slice()
works like substring()
with a few different behaviors.
Syntax: string.slice(start, stop);
Syntax: string.substring(start, stop);
他们有什么共同点:
开始
等于停止
:返回一个空字符串 stop
:将字符提取到字符串的末尾start
equals stop
: returns an empty stringstop
is omitted: extracts characters to the end of the string 区别 substring()
:
开始>停止
,然后 substring
将交换这两个参数。 NaN
,它被视为 0
。start > stop
, then substring
will swap those 2 arguments.NaN
, it is treated as if it were 0
. 区分 slice()
:
开始>停止
, slice()
将返回空字符串。 (
)开始
为否定:设置来自的字符串的结尾,与Firefox中的 substr()
完全相同。在Firefox和IE中都会出现此行为。 stop
为负:将stop设置为: string .length - Math.abs(停止)
(原始值),但限制为0(因此, Math.max(0,string.length + stop)$ c ECMA中涵盖的$ c>)规范。
start > stop
, slice()
will return the empty string. (""
)start
is negative: sets char from the end of string, exactly like substr()
in Firefox. This behavior is observed in both Firefox and IE.stop
is negative: sets stop to: string.length – Math.abs(stop)
(original value), except bounded at 0 (thus, Math.max(0, string.length + stop)
) as covered in the ECMA specification.