The usage of indexof in JS detailed parsing _javascript techniques

Source: Internet
Author: User

The

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

reports the index of the first occurrence of the specified character in this instance. The search starts at the specified character position and checks the specified number of character positions. The Unicode character to find for

parameters
 
Value

. Searches for value are case-sensitive.

startIndex (Int32)
 
to search for the starting position. is not set to start at 0.

count (Int32)
 
Optional, the number of character positions to check.

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

IndexOf ()
 
Find the first occurrence of a specified character or string in a string, and return to the first index value, such as:

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

str1. IndexOf ("string");//Find the index value (position)

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

The indexof parameter is string, looking for the first occurrence of the parameter 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 functionality, you should use a Regex class to match strings using regular expressions.

IndexOf (): Locates characters and strings in a string from the previous position; all return values are in absolute positions of strings, such as 1

string test= "asdfjsdfjgkfasdsfsgfhgjgfjgdddd";

Test.indexof (' d ') = 2//pre-position D first occurrence

Test.indexof (' d ', 1) = 2///previous position d from the first occurrence of the third string

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

LastIndexOf (): Position characters and strings in strings;,

Usage and IndexOf () is exactly the same.

below describes IndexOfAny | | Lastindexofany
 
They accept the character array as a variable, and the other methods are as follows, returning the first subscript position of any character in the array

below
&NBSP
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.

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.