Home  >  Article  >  Web Front-end  >  Comparison of the differences between substring&substr&slice in js

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

不言
不言Original
2018-08-23 14:30:392523browse

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

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!

Statement:
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