The indexOf() method returns the position of the first occurrence of a specified string value in the string.
lastIndexOf() method can return the last occurrence position of a specified string value, searching from back to front at the specified position in a string.
Thesubstring() method is used to extract characters between two specified subscripts in a string.
substr(start,length) means starting from the start position, intercepting a string of length length
split splits a string into substrings and returns the result as a string array
replace is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression
1.substr
substr(start,length) means starting from the start position, intercepting a string of length length.
var src="images/off_1.png";
alert(src.substr(7,3));
The pop-up value is: off
2.substring
substring(start,end) represents the string from start to end, including the character at the start position but excluding the character at the end position.
var src="images/off_1.png";
alert(src.substring(7,10));
The pop-up value is: off
3.indexOF
The indexOf() method returns the position (from left to right) where a specified string value first appears in the string. If there is no match, -1 is returned, otherwise the subscript value of the string at which the first occurrence occurs is returned.
var src="images/off_1.png";
alert(src.indexOf('t'));
alert(src.indexOf('i'));
alert(src.indexOf('g'));
The pop-up values are: -1,0,3
4.lastIndexOf
The lastIndexOf() method returns the first character index value of a certain character or string from right to left (opposite of indexOf)
var src="images/off_1.png";
alert(src.lastIndexOf('/'));
alert(src.lastIndexOf('g'));
The pop-up values are: 6, 15
5.split
Split a string into substrings and return the result as an array of strings.
Return a string separated by spaces and return
function SplitDemo(){ var s, ss; var s = "The rain in Spain falls mainly in the plain."; // 在每个空格字符处进行分解。 ss = s.split(" "); return(ss); }
6.replace:
is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression.
Syntax: stringObject.replace(regexp, replacement);
Parameters:
regexp: required, the RegExp object of the pattern to be replaced
replacement: required, replacement text or function to generate replacement text
Return value:
A new string obtained by replacing the first match or all subsequent matches of regexp with replacement.
Description:
The replace() method of string stringObject performs a search and replace operation. It will look for substrings in stringObject that match regexp and replace those substrings with replacement. If the regexp has the global flag g, then the replace() method replaces all matching substrings. Otherwise, it only replaces the first matching substring.
The above is a detailed explanation of the usage of substr, substring, indexOf, lastIndexOf, split and replace in js introduced in this article. I hope you like it.