Differences between substring and substr in js _ javascript skills

Source: Internet
Author: User
This article mainly introduces the differences and usage of substring and substr in js. Each step has a corresponding text description. If you are interested, refer to before you start. Review the subscript in js (array element/character subscript in string ):
The subscript is always counted from 0, for example
Var arr = [, 3]; // The array length is 3, and the element subscript is:, 2
Arr [0] = 1, arr [1] = 2 ..
String: for example, var s = "hello"; // the string length is 5, the subscript of the first character 'H' is 0, and so on.

String. substring (): returns the substring of a String.
Usage: string. substring (from,)
Here, from refers to the position of the first character of the substring to be extracted in the original string.
To refers to the last character of the substring to be extracted (this parameter can be left blank)

NextString. substring () for example:

1. string. substring (from ):This is equivalent to intercepting from the from position to the end of the original string.

Var s = "hello"; s. substring (1); // It starts from the character whose subscript is 1 (here it is 'E') and ends at the end of the string, and finally gets the substring "ello"


2. string. substring (from, ):From the from position to the to-1 position

Var s = "hello"; s. substring (); // It is equivalent to intercepting a character from the position of 1 to a character of the position of 2. The substring is: "el"

String. substr (): it is used to extract a substring, but it is different from the String. substring () above.
Usage: string. substr (start, length)
Start: refers to the start subscript of the substring.
Length: Specifies the length of the substring)
1. string. substr (start, length ):Here is an example:

Var s = "hello"; s. substr (); // truncate three characters starting from the character with the subscript of 1. The last substring is: ell

Two special cases are added:
The second and second parameters exceed the remaining characters

Var s = "hello"; s. substr () // In this case, the default position is from, start to the end of the original string, that is, return: "ello"

B. The first parameter is negative.
In this case, the value-1 indicates the last character of the string, and-2 indicates the second to last character.

Var s = "hello"; s. substr (-3, 2) // extract two lengths starting from the last three characters and get: "ll"

2. string. substr (start): Without the length parameter, the default value is to truncate the string from the start position to the end of the string.

var s = "hello";s.substr(3)//"lo"

The above is a detailed introduction to the differences and usage of substring and substr in js. You can learn it in conjunction with the previous articles, hoping to help you learn it.

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.