How to Use substr, substring, indexOf, lastIndexOf, split, and replace in js _ javascript skills

Source: Internet
Author: User
This article mainly introduces details about how to use substr, substring, indexOf, lastIndexOf, split, and replace in js. For more information, see indexOf () returns the position of the first occurrence of a specified string value.

The lastIndexOf () method returns the last position of a specified string value and searches forward from the specified position in the string.

The substring () method is used to extract characters of a string between two specified subscripts.

Substr (start, length) indicates to extract the length string from the start position.

Split Splits a string into substrings and returns the result as a string array.

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

1. substr

Substr (start, length) indicates that the length string is truncated from the start position.

Var src = "images/off_1.png ";
Alert (src. substr (7,3 ));

Pop-up value: off

2. substring

Substring (start, end) indicates the string between start and end, including the start position but not the end position.

Var src = "images/off_1.png ";
Alert (src. substring (7,10 ));

Pop-up value: off

3. indexOF

The indexOf () method returns the position (from left to right) where a specified string value first appears in the string ). If no match exists,-1 is returned. Otherwise, the value of the string that appears for the first time 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, and 3.

4. lastIndexOf

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

Var src = "images/off_1.png ";
Alert (src. lastIndexOf ('/'));
Alert (src. lastIndexOf ('G '));

Pop-up values: 6, 15

5. split

Splits a string into substrings and returns the result as a string array.

Returns a string separated by spaces.

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

6. replace:

It 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. RegExp object of the pattern to be replaced.

Replacement: required. Replace text or generate a function to replace text.

Return Value:

A new string, replacement is used to replace the first match of regexp or all matching results.

Note:

The replace () method of the string stringObject performs the search and replace operation. It searches stringObject for substrings that match regexp and replaces them with replacement. If regexp has a global flag, the replace () method replaces all matched substrings. Otherwise, it only replaces the first matched substring.

The above section describes how to use substr, substring, indexOf, lastIndexOf, split, and replace in js.

Related Article

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.