Introduction to the differences between indexof and indexofany
- 2020-05-30 19:48:40
- OfStack
To locate a substring is to look for a substring or a character contained in a string. In the String class, common methods for locating substrings and characters include IndexOf/LastIndexOf and IndexOfAny/LastIndexOfAny, 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. It is case sensitive and counts at 0 from the first character of the string. If the string does not contain this character or substring, -1 is returned. The common overloaded form is shown below.
(1) positioning character:
int IndexOf(char value)
int IndexOf(char value, int startIndex)
int IndexOf(char value, int startIndex, int count)
(2) location substring:
int IndexOf(string value)
int IndexOf(string value, int startIndex)
int IndexOf(string value, int startIndex, int count)
In the above overloaded form, the parameters have the following meanings:
value: character or substring to be determined.
startIndex: the actual location to start searching in the total string.
count: the number of characters in the total string searched from the beginning.
The following code looks for the first occurrence of the character 'l' in' Hello '.
Code 4-7 USES IndexOf to find the first occurrence of the 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 particular character or substring in a string. Its method definition and return value are the same as IndexOf.
2. IndexOfAny/LastIndexOfAny
The IndexOfAny method is similar to IndexOf in that it can search for the first occurrence of any character in a string in an array of 1 characters. Again, this method is case sensitive and counts at 0 starting with the first character of the string. If the string does not contain this character or substring, -1 is returned. There are three common forms of IndexOfAny overloading:
(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 parameters have the following meanings:
(1) anyOf: undetermined bit character array. The method will return the first occurrence of any character in the array.
(2) startIndex: the actual position to search in the original string.
(3) count: the number of characters searched from the beginning of the original string.
The following example looks for the first and last occurrence of the character 'l' in' Hello '.
Code 4-8 USES IndexOfAny to find the first and last occurrence 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 occurrence of any character in a string in an array of 1 character.