Home > Web Front-end > JS Tutorial > Analysis of slice() method of String object in javascript

Analysis of slice() method of String object in javascript

PHPz
Release: 2018-09-30 09:48:36
Original
1759 people have browsed it

This article analyzes the slice() method of String object in JavaScript in more detail. Share it with everyone for your reference. The specific analysis is as follows:

This method intercepts a section of the string and returns a new string composed of the intercepted characters.

This article mainly introduces the slice() method of String object in JavaScript. It analyzes the definition, parameters and specific usage of the slice() method in the form of examples. It has certain reference value. Friends who need it can Please refer to the following

Note: The original string will not change, and the return value is a newly generated string.

Syntax structure:

stringObject.slice(start,end)
Copy after login

Parameter list:

参数描述
start 必需。规定从何处开始截取字符串。字符串的首字符的位置是0。
如果此参数为负数,那么将从字符串的尾部开始计算位置。例如:-1代表倒数第一个字符,-2代表倒数第二个字符,以此类推。
end 可选。规定在何处结束截取字符串。
如果省略该参数,那么将截取从start位置开始到结尾的所有字符。
注:end对应的字符不会被截取。

Example code:

Example 1:

var a="abcdefgmnlxyz";
console.log(a.slice(2,3));
Copy after login

Intercept the string between position "2" and position "3", but the character d corresponding to position "3" is not included in the interception return. Output result: c.

Example 2:

var a="abcdefgmnlxyz";
console.log(a.slice(2));
Copy after login

If the second parameter is omitted, all characters from position "2" to the end of the string will be intercepted. Output result: cdefgmnlxyz.

I hope this article will be helpful to everyone’s JavaScript programming design. For more related tutorials, please visit JavaScript Video Tutorial!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template