The difference between substring and substr in JS and how to use them _javascript skills

Source: Internet
Author: User

Before you begin, review the next JS subscript (array element/string character Poute):
The subscript always counts starting at 0, for example
var arr = [The length of the 1,2,3];//array is 3, and the element subscript is: 0,1,2
Arr[0] = 1,arr[1]=2.
String similar to: var s = "Hello";/String length 5, first character ' H ' subscript 0, and so on

String.substring (): is used to return a substring of a string
The usage is as follows: String.substring (from, to)
The position of the first character in the original string from the substring to be drawn.
To refers to the end of the last character of the substring to be drawn (this parameter may not be added)

Here are some examples of string.substring () :

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

var s = "Hello";
S.substring (1)//is from the Subscript 1 character (here is ' e ') start to the end of the string all intercept, finally get substring "ello"


2, String.substring (from, to): interception from the from location to to-1 position

var s = "Hello";
S.substring (1,3)//is equivalent to the character from position 1 to the position of 2, and the substring is: "El"

String.substr (): The function is also to draw a substring, but it differs from the string.substring () above
Usage is as follows: String.substr (start, length)
Start: Refers to the intercept substring starts subscript
Length: Intercepts the lengths of substrings (can be omitted)
1, string.substr (start, length): First, give an example to illustrate:

var s = "Hello";
S.SUBSTR (1,3)//The 3 character length is intercepted from the character labeled 1, and the last substring is: ell

Add two special cases:
A, the second parameter exceeds the remaining character length

var s = "Hello";
S.SUBSTR (1,7)//In this case, the default from, start position to the end of the original string, that is, return: "Ello"

B, the first argument is a negative number
In this case, starting at the end of the string, 1 refers to the last character of the string--2 to the penultimate character ... By analogy

var s = "Hello";
S.SUBSTR ( -3,2)//That is, starting from the last third character intercept 2 lengths, get: "ll"

2, String.substr (start) : The default is to intercept from the start position to the end of the string without the length parameter

var s = "Hello";
S.SUBSTR (3)//"Lo"

The above is about JS substring and substr differences and usage of the detailed introduction, we can combine the relevant articles before the study, I hope to help you learn.

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.