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:
The code is as follows: |
Copy code |
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 ("string", 4th); // search for the index value (location) of the first character of the "string" in str1 starting from str1 characters) |
Instance
The code is as follows: |
Copy code |
String str = "OK is OK "; Console. WriteLine (str. IndexOf ("OK ")); Console. WriteLine (str. IndexOf ("OK", 1 )); |
Microsoft.com website description
The following example shows how to use the indexOf function to find the index location of a specified item. The returned index is the first occurrence of the specified item in the item. Call this function again and specify the index value greater than the index value of the element to be found. You can find the next appearance of the item.
JScript
The code is as follows: |
Copy code |
Var a = ['red', 'blue', 'green', 'blue']; Var myFirstIndex = Array. indexOf (a, "blue "); // View the results: "1" Alert ("myFirstIndex:" + myFirstIndex ); Var mySecondIndex = Array. indexOf (a, "blue", (myFirstIndex + 1 )); // View the results: "3" Alert ("mySecondIndex:" + mySecondIndex ); |
PH
The code is as follows: |
Copy code |
Var a = ['red', 'blue', 'green', 'blue']; Var myFirstIndex = Array. indexOf (a, "blue "); // View the results: "1" Alert ("myFirstIndex:" + myFirstIndex ); Var mySecondIndex = Array. indexOf (a, "blue", (myFirstIndex + 1 )); // View the results: "3" Alert ("mySecondIndex:" + mySecondIndex ); |