Detailed analysis of indexof usage in js

Source: Internet
Author: User

Detailed analysis of indexof usage in js

This article mainly introduces the use of indexof in js in detail. If you need a friend, please refer to it and hope to help you.

String. IndexOf method (Char, [startIndex], [count])

 

The index of the first matching item of the specified character in this instance. Searches start from the specified character location and checks the specified number of character locations.

 

Parameters

 

Value

 

Unicode character to be searched. The search area of value is case sensitive.

 

StartIndex (Int32)

 

Optional. Start position of the search. If this parameter is not set, it starts from 0.

 

Count (Int32)

 

Optional. number of characters to be checked.

 

Return Value

 

If this character is found, it is the index position of the value; otherwise, it is-1.

 

IndexOf ()

 

Searches for the position of the specified character or string that appears for the first time in the string, and returns the first index value, for example:

 

Str1.IndexOf ("word"); // search for the index value (location) of the word in str1)

 

Str1.IndexOf ("string"); // search for the index value (location) of the first character of "string" in str1)

 

Str1.IndexOf ("Word", start, end); // query end characters starting from start + 1 in str1, search for the position of "word" in STR1 [starting from the first character] Note: start + end cannot exceed the length of str1

 

The indexof parameter is string. Search for the position where the parameter string appears for the first time in the string and return the position. For example, string s = "0123 dfdfdf"; int I = s. indexof ("df"); then I = 4.

 

If you need more powerful string parsing functions, use the Regex class and use regular expressions to match strings.

 

Indexof (): locates the character and string from the front to the back in the string. All return values are absolute positions in the string. If it is null, it is-1.

 

String test = "asdfjsdfjgkfasdsfsgfhgjgfjgdddd ";

 

Test. indexof ('D') = 2 // locate the location where d appears for the first time.

 

Test. indexof ('D', 1) = 2 // locate the first position where d appears from the third string

 

Test. indexof ('D', 5, 2) = 6 // locate d from the front to the back and query it from 5th bits. Check for 2 bits, that is, from 5th bits to 7th bits;

 

Lastindexof (): locates the character and string from the back and forward in the string ;,

 

The usage is exactly the same as that of indexof.

 

The following describes IndexOfAny | lastindexofany.

 

They accept the character array as the variable element. The other methods are the same as above, and return the subscript location of any character in the array.

 

As follows:

 

Char [] bbv = {'s ', 'C',' B '};

 

String abc = "acsdfgdfgchacstcsad ";

 

Response. Write (abc. IndexOfAny (bbv) = 1

 

Response. Write (abc. IndexOfAny (bbv, 5) = 9

 

Response. Write (abc. IndexOfAny (bbv, 5, 3) = 9

 

Lastindexofany is the same as above.

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.