js substr substring indexOf lastIndexOf split replace

  • 2020-10-23 20:50:49
  • OfStack

The indexOf() method returns the location of the first occurrence of a specified string value in a string.

The lastIndexOf() method returns the last occurrence of a specified string value, and searches back and forth from the specified position in the string.

The substring() method is used to extract the characters that the string mediates between two specified subscripts.

substr(start,length) means to intercept a string of length length, starting at the start location

split splits a string into substrings and returns the result as an array of strings

replace is used to replace one character in a string with another, or to replace a substring that matches a regular expression

1.substr

substr(start,length) means to intercept the length length string from the start position.

var src="images/off_1.png";
alert(src.substr(7,3));

The pop-up value is: off

2.substring

substring(start,end) represents a string from start to end, including characters at start but not at end.

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 (left to right) where a specified string value first appears in a string. If there is no match, -1 is returned; otherwise, the subscript value of the string where the position first appeared 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, in turn, -1,0,3

4.lastIndexOf

The lastIndexOf() method returns the first character index of a character or string that appears from right to left (as opposed to indexOf)

var src="images/off_1.png";
alert(src.lastIndexOf('/'));
alert(src.lastIndexOf('g'));

The pop-up values are, in turn, 6,15

5.split

Splits a string into substrings, and returns the result as an array of strings.

Returns 1 string split by space


function SplitDemo(){
  var s, ss;
  var s = "The rain in Spain falls mainly in the plain.";
  //  Decompose at each space character. 
  ss = s.split(" ");
  return(ss);
}

6.replace:

Used to replace 1 character in a string with another character, or to replace 1 substring that matches a regular expression.

Grammar: stringObject.replace(regexp, replacement);

Parameters:

regexp: Required, RegExp object of the schema to be replaced

replacement: Function required to replace text or generate replacement text

The return value:

1 new string, replacing the first match or all matches of regexp with replacement.

Description:

The replace() method of the string stringObject performs a find and replace operation. It looks in stringObject for substrings that match regexp and replaces those substrings with replacement. If regexp has the global flag g, the replace() method replaces all matching substrings. Otherwise, it replaces only the first matching substring.

Above is substr, in this article to introduce the js substring, indexOf, lastIndexOf, split and replace usage explanation, hope you like them.


Related articles: