JavaScript中substr、substring、slice和splice的区别与用法详解

作者:函数茶馆时间:2026-10-25 14:26:12浏览:421

本文详细解析JavaScript中字符串截取方法substr、substring和slice的区别,以及数组操作splice的用法。重点对比各方法的参数含义、负数处理机制及返回值差异,并提供代码示例说明不同场景下的行为,帮助开发者避免常见错误。

substr 方法详解

substr() 方法用于从字符串中提取从指定 start 下标开始的指定数量的字符。其语法为 stringObject.substr(start, length),其中 start 参数是必须的,而 length 参数是可选的。

start 参数指定截取的起始位置下标,从 0 开始计数,必须为数字类型。该参数支持负数,例如 -1 代表倒数第一个字符,-2 代表倒数第二个字符,依此类推。

length 参数指定要截取的字符数量,必须为数字。如果省略此参数,则默认从 start 位置截取至字符串末尾。

由于 substr 通过指定起始下标和截取长度来工作,因此在功能上可以替代 substring 和 slice 方法。

重要提示:ECMAScript 标准并未对该方法进行标准化,因此官方建议避免使用该方法。

substring 方法详解

substring() 方法用于提取字符串中位于两个指定下标之间的字符。

其语法为 stringObject.substring(start, end),其中 start 参数是必须的,end 参数是可选的。

start 参数指定截取的起始位置下标,从 0 开始计数,必须为非负数字。尽管 W3C 规范指出必须是非负整数,但实际测试显示非整数也可被接受。

end 参数必须是数字。如果未指定,则默认从 start 位置截取至字符串末尾。

注意事项:substring 方法截取的字符不包含 end 位置处的字符,因此实际截取长度为 end 减去 start。

当 start 等于 end 时,该方法返回空字符串,长度为 0。

重要提示:substring 不接受负数参数,而 slice 和 substr 方法可以处理负数。

slice 方法详解

slice() 方法用于提取字符串的某一部分,并返回一个新的字符串。

其语法为 stringObject.slice(start, end),其中 start 参数是必须的,end 参数是可选的。

start 参数指定提取片段的起始下标。如果为负数,则表示从字符串尾部开始计数,例如 -1 指最后一个字符,-2 指倒数第二个字符。

end 参数指定提取片段的结束下标。如果未指定,则提取从 start 到字符串末尾的所有字符。若 end 为负数,同样表示从字符串尾部开始计数。

splice 方法详解

splice() 方法用于对数组进行插入、删除或替换元素的操作。

其语法为 arrayObject.splice(index, howmany, element1, ..., elementX),其中 index 和 howmany 是必须的参数,其他元素为可选。

index 参数规定从何处添加或删除元素,即开始插入或删除的数组元素下标,必须为数字。

 

howmany 参数规定应该删除多少个元素,必须为数字,但可以是 "0"。如果未指定此参数,则删除从 index 开始到原数组结尾的所有元素。

element1 参数规定要添加到数组的新元素,从 index 所指的下标处开始插入。

elementx 参数允许向数组添加多个元素。

注释:请注意,splice() 方法与 slice() 方法的作用不同,splice() 会直接修改原数组。

提示:在某些情况下,负数参数可能不被识别。因此建议尽量避免使用负数作为参数,以免因浏览器兼容性导致程序出错。


 

以上内容基于《JavaScript 权威指南》的说明,由作者根据理解进行解释,并非直接翻译。


string.substring(from, to)

Arguments

from

A nonnegative integer that specifies the position within string of the first character of the desired substring.

       指定想要得到字符串的开始位置,即索引(非负整数)

to

A nonnegative optional integer that is one greater than the position of the last character of the desired substring. If this argument is omitted, the returned substring runs to the end of the string.

指定想要得到字符串的结束位置,不包括该位置的字符(非负整数,可选,没有指定则返回从指定开始位置到原字符串结束位置)



string.slice(start, end)

Arguments

start

The string index where the slice is to begin. If negative, this argument specifies a position measured from the end of the string. That is, -1 indicates the last character, -2 indicates the second from last character, and so on.

        指定想要得到字符串的开始的位置,即索引。

end

The string index immediately after the end of the slice. If not specified, the slice includes all characters from start to the end of the string. If this argument is negative, it specifies a position measured from the end of the string.

指定想要得到字符串的结束位置,不包括该位置的字符(可选,没有指定则返回从指定开始位置到原字符串结束位置)


string.substr(start, length)

Arguments

start

The start position of the substring. If this argument is negative, it specifies a position measured from the end of the string: -1 specifies the last character, -2 specifies the second-to-last character, and so on.

        指定想要得到字符串的开始的位置,即索引。

length

The number of characters in the substring. If this argument is omitted, the returned substring includes all characters from the starting position to the end of the string.

指定想要得到字符串的长度,(可选,没有指定则返回从指定开始位置到原字符串结束位置)

参数处理对比与示例

PS:这三个方法均返回截取原字符串的一部分新字符串,第2个参数均为可选参数,并且其实所有的参数均可以为负整数。 string.substring(from, to) string.slice(start, end) 这两个方法差不多,都是指定开始和结束位置返回新字符串,在参数均为正整数的时候返回结果一样,当参数为负整数的时候,string.substring(from, to) 把负整数都当作0处理,而 string.slice(start, end) 将把负整数加上该字符串的长度处理。 string.substr(start, length) 这个方法只在第二个参数上指定的是新字符串的长度,对于负正数和string.slice(start, end)处理一样,把负整数加上原字符串的长度。 Example

代码如下:

var s = "abcdefg";
s.substring(1,4) // Returns "bcd"
s.slice(1,4) // Returns "bcd"
s.substr(1,4) // Returns "bcde"
s.substring(2,-3) // Returns "ab" 实际上是 s.substring(0,2) 较小的参数会在前面
s.slice(2,-3) // Returns "cd" 实际上是 s.slice(2,4)
s.substr(2,-3) // Returns "cdef" 实际上是 s.slice(2,4)
作者最新文章