js commonly used method of dividing and fetching strings

1.substr

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

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

弹出值为:off

2.substring

substring(start,end) represents the string from start to end, including the character at the start position but not the character at the end position.

var src="images/off_1.png";
alert(src.substring(7,10));

弹出值为: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, return -1, otherwise return the subscript value of the string at the first occurrence position.

var src="images/off_1.png";
alert(src.indexOf('t'));
alert(src.indexOf('i'));
alert(src.indexOf('g'));

弹出值依次为:-1,0,3

4.lastIndexOf

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

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

弹出值依次为:6,15

5.split

Split a string into substrings, and then return the result as an array of strings.
Return a string separated by spaces

function SplitDemo(){
    
    
  var s, ss;
  var s = "The rain in Spain falls mainly in the plain.";
  // 在每个空格字符处进行分解。
  ss = s.split(" ");
  return(ss);
}

Guess you like

Origin blog.csdn.net/weixin_46099269/article/details/112003703