Home > Web Front-end > JS Tutorial > body text

Comparison of the differences between substring&substr&slice in js

不言
Release: 2018-08-23 14:30:39
Original
2539 people have browsed it

The content of this article is about the difference and comparison between substring&substr&slice in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

substring(), substr(), slice(), please tell me the differences between the three types of parsing strings, and their applicable situations?

Answer: substring() returns a string , the string consists of characters between the two points specified by the start and end parameters. If the end parameter is not specified, the end of the substring is the end of the string. If the value of start is equal to the value of end, this method returns an empty string. If the value of start is greater than the value of end, the two parameters will be automatically interchanged before the function is executed, and the original values ​​will remain unchanged.

Substr: Returns the characters in the string starting from the index specified by the start parameter and ending with the number of characters specified by the length parameter. The substr method does not change the string specified by my_str; it returns a new string.

Slice: Returns a string that includes all characters from the start character up to (but not including) the end character. The original String object is not modified. If the end parameter is not specified, the end of the substring is the end of the string. This method returns an empty string if the character indexed by start is the same as or to the right of the character indexed by end.

var a:Array=[0,1,2,3,4,5,6,7,8,9]
var str:String=a.join("");
trace(str)trace(str.slice(3,6))trace(str.substr(3,3))trace(str.substring(3,6))trace(str)0123456789
345
345
345
0123456789
Copy after login

Related recommendations:

Usage of substring and substr in JS

substring(), substr() in javascript , The difference between slice()_javascript skills

The above is the detailed content of Comparison of the differences between substring&substr&slice in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!