Asp.net's IndexOf, LastIndexOf, IndexOfAny and LastIndexOfAny usage

Source: Internet
Author: User

Locate a substring is to find the substring or character contained in a string. Next, we will introduce it in detail.

  I. IndexOf/LastIndexOf

The IndexOf method is used to search for the first occurrence of a specific character or substring in a string. This method is case sensitive and starts from the first character of the string and starts with 0. If the string does not contain this character or substring,-1 is returned. The following is a common heavy load method.

① Positioning characters

Int IndexOf (char value)

Int IndexOf (char value, int startIndex)

Int IndexOf (char value, int startIndex, int count)

② Locate substrings

Int IndexOf (string value)

Int IndexOf (string value, int startIndex)

Int IndexOf (string value, int startIndex, int count)

In the above overload form, the parameter meanings are as follows:

Value: a character or substring of a given digit.

StartIndex: the actual position of the Start search in the total string.

Count: the number of characters from the start position in the total string.

The following code searches for the first occurrence location of the character 'l' in "Hello.

Code 4-7 use IndexOf to find the first occurrence location of a character: Default. aspx. cs

1. String s = "Hello ";

2. int I = s. IndexOf ('l'); // 2

Similar to IndexOf, LastIndexOf is used to search for the last occurrence of a specific character or substring in a string. Its legal meaning and return values are the same as IndexOf and will not be repeated.

  2. IndexOfAny/LastIndexOfAny

The IndexOfAny method is similar to IndexOf. The difference is that it can search for the first position of any character in a string that appears in a character array. Similarly, this method is case sensitive and starts from the first character of a string and starts with 0. If the string does not contain this character or substring,-1 is returned. There are three common IndexOfAny reloads:

(1) int IndexOfAny (char [] anyOf );

(2) int IndexOfAny (char [] anyOf, int startIndex );

(3) int IndexOfAny (char [] anyOf, int startIndex, int count ).

In the above overload form, the parameter meanings are as follows:

(1) anyOf: A String Array of characters to be determined. The method returns the position where any character in the array appears for the first time.

(2) startIndex: the actual position where the search starts from the original string.

(3) count: number of characters searched from the start position in the original string.

In the following example, find the first and last occurrence location of the character 'l' in "Hello.

Code 4-8 Use IndexOfAny to find the first and last occurrence location of the substring: Default. aspx. cs

1. String s = "Hello ";

2. char [] anyOf = {'h', 'E', 'L '};

3. int i1 = s. IndexOfAny (anyOf); // 0

4. int i2 = s. LastIndexOfAny (anyOf); // 3

Similar to IndexOfAny, LastIndexOfAny is used to search for the last position of any character in a character array.

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.