A detailed analysis of the usage of indexof in JS

Source: Internet
Author: User

This article is mainly to JS in the use of indexof in a detailed introduction, the need for friends can come to the reference, I hope to be helpful to everyone

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

Reports the index of the first occurrence of a specified character in this instance. The search starts at the specified character position and checks the specified number of character positions.

parameters

Value

The Unicode character to find. The search for value is case-sensitive.

StartIndex (Int32)

Option to search for the starting position. No setting starts from 0.

count (Int32)

Optional, the number of character positions to check.

return value

The index position of value if the character is found, or 1 if not found.

IndexOf ()

Finds the first occurrence of a specified character or string in a string, and returns the index value, such as:

Str1. IndexOf ("word");//Find the index value of "word" in str1 (position)

Str1. IndexOf ("string");//Find the index value (position) of the first character of the string in str1

Str1. IndexOf ("word", start,end);//from str1 start+1 characters, look for end characters, find the position of "word" in string STR1 [from the first character] Note: Start+end cannot be greater than str1 length

The indexof parameter is string, looking for the position of the first occurrence of the argument string in the string and returning the position. such as String s= "0123DFDFDF"; int i=s.indexof ("df"), then i==4.

If you need more powerful string parsing, you should use a Regex class to match strings with regular expressions.

IndexOf (): Position characters and strings from the front to back in a string; all return values refer to the absolute position of the string, or 1 if null.

String test= "asdfjsdfjgkfasdsfsgfhgjgfjgdddd";

Test.indexof (' d ') = 2//position from front to back for first occurrence of D

Test.indexof (' d ', 1) = 2//front-to-back position d from the first occurrence of a third string

Test.indexof (' d ', 5,2) = 6//front-to-back positioning D from 5th position, check 2, that is, from 5th to 7th;

LastIndexOf (): positions characters and strings from backward to forward in a string;

Usage is exactly the same as indexof ().

The following introduction IndexOfAny | | Lastindexofany

They accept the character array as a variable, as in the other way, returning any one of the characters in the array to the first occurrence of the subscript position

as follows

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

String abc = "Acsdfgdfgchacscdsad";

Response.Write (ABC. IndexOfAny (BBV)) =1

Response.Write (ABC. IndexOfAny (BBV, 5)) =9

Response.Write (ABC. IndexOfAny (BBV, 5, 3)) =9

Lastindexofany Ibid.


A detailed analysis of the usage of indexof in JS

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.