Asp.net indexOf () to find the position of the specified string in the string

Source: Internet
Author: User

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 );
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.