IndexOf and IndexOfAny The difference introduction _ Practical Skills

Source: Internet
Author: User

Locating a substring is the search for a string that contains a substring or a character. In the string class, the common methods for locating substrings and characters include Indexof/lastindexof and Indexofany/lastindexofany, which are described in detail below.
1. Indexof/lastindexof
The IndexOf method is used to search for the first occurrence of a particular character or substring in a string, which is case-sensitive and counts 0 from the first character of the string. Returns-1 if the string does not contain this character or substring. The commonly used overloaded forms are shown below.

(1) Positioning character:

int IndexOf (char value)

int IndexOf (char value, int startIndex)

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

(2) Set seat string:

int IndexOf (string value)

int IndexOf (string value, int startIndex)

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

In the above overloaded form, the parameter meaning is as follows:

Value: The character or substring to be positioned.

StartIndex: The fact that the search begins in the total string.

Count: The number of characters that are searched from the start position in the total string.

The following code looks for the first occurrence of the character ' L ' in "Hello".

Code 4-7 using IndexOf to find the first occurrence of a character position: 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 particular character or substring in a string, and its method definition and return values are the same as indexof and no longer repeat.

2. Indexofany/lastindexofany
The IndexOfAny method function is similar to indexof, except that it can search for the first occurrence of any character in a character array that appears in a string. Similarly, the method is case-sensitive and starts with a 0 count from the first character of the string. Returns-1 if the string does not contain this character or substring. There are 3 types of commonly used IndexOfAny overload forms:

(1) int indexofany (char[]anyof);

(2) int indexofany (char[]anyof, int startIndex);

(3) int indexofany (char[]anyof, int startIndex, int count).

In the above overloaded form, the parameter meaning is as follows:

(1) anyof: An array of characters to be positioned, and the method returns the first occurrence of any character in the array.

(2) StartIndex: The fact that the search begins in the original string.

(3) Count: The number of characters to search from the start position in the original string.

The following example looks for the first and last occurrences of the character ' L ' in "Hello".

Code 4-8 using IndexOfAny to find the first and last occurrence of a 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 occurrence of any character in a character array that appears in a string.

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.