The usage of substr,substring,indexof,lastindexof,split,replace in JS _javascript skills

Source: Internet
Author: User

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, which is searched forward at the specified position in a string.

The substring () method extracts characters from a string mediation between two specified subscripts.

SUBSTR (start,length) indicates that the length string is intercepted from the start position

Split splits a string into a substring and then returns the result as an array of strings

Replace is used to replace characters with some characters in a string, or to replace a substring that matches a regular expression

1.substr

SUBSTR (start,length) indicates that the length of a string is intercepted 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 from the start position but not 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 first occurrence of a specified string value in a string, from left to right. If no match is returned-1, otherwise returns the subscript value of the string for the first occurrence of the position.

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 character or string that appears from right to left (contrary to indexof)

var src= "Images/off_1.png";
Alert (Src.lastindexof ('/'));
Alert (Src.lastindexof (' G '));

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

5.split

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

Returns a string with a space split

function Splitdemo () {
  var s, SS;
  var s = "The rain in Spain falls mainly in the plain.";
  Decomposes at each space character.
  ss = S.split ("");
  return (ss);
}

6.replace:

Used to replace some characters in a string with some other character, or to replace a substring that matches a regular expression.

Grammar: Stringobject.replace (regexp, replacement);

Parameters:

RegExp: Required, RegExp object for the pattern to be replaced

Replacement: required, replace text or generate alternate text function

return value:

A new string that is replaced with replacement for the first match of RegExp or after all matches have been made.

Description

The replace () method of the string Stringobject performs a find-and-replace operation. It will look for substrings in the stringobject that match the regexp, and then replace them with replacement. If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matching substring.

The above is described in this article for you to introduce the JS in Substr,substring,indexof,lastindexof,split and replace the use of detailed, I hope you like.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.