The difference between JS indexOf () lastIndexOf () and substring () intercept strings

Source: Internet
Author: User

1. String.IndexOf Method (Value[,startindex])

Value: The Unicode character to find. Required Options
StartIndex: Search start location. Option not to write from the beginning to find

The method returns an integer value,

Indicates the starting position of a substring in a string object, if no second index parameter is set, starting from subscript 0. If no substring is found, 1 is returned.

2.string.lastindexof (Char,int32)

Char: the string to find

Int32: Find within a specified range

The method returns an integer value,

Indicates the last index position of the substring of a string object, starting at subscript 0. If no substring is found, 1 is returned.

If you set the second argument, it represents the last index position within the specified range

For example:

string= "Hello Nihao a!"

String. LastIndexOf (' Nihao '); Returns 6

String. LastIndexOf (' Nihao ', 5); Returns-1

String. LastIndexOf (' Nihao ', 8); Returns 6

Finds the character within the specified range, which is the input parameter above, and is understood as the position of the last matched string, starting at index 0 to the specified numeric position range.

3.string.substring (Indexstar[,indexend])

You can write only one parameter: the string that follows the intercept from the index value

Write two parameters: A string that intercepts the start of the specified index between the end of the specified index (excluding the end index)

String= "Liming is a good boy"

String.substring (3); Back: ing is a good boy

String.substring (3,6); Return: ing that is subscript 3 start to subscript 5 end of string

The difference between JS indexOf () lastIndexOf () and substring () intercept strings

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.