System.String.IndexOf Method

Reports the zero-based index of the first occurrence of the specified string in this instance. The search starts at a specified character position and examines a specified number of character positions.


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


The string to seek.
The search starting position.
The number of character positions to examine.


The zero-based index position of value if that string is found, or -1 if it is not. If value is string.Empty, the return value is startIndex.


ArgumentNullException value is a null reference.

startIndex or count is negative


startIndex + count is greater than the length of the current instance.


Index numbering starts from 0 (zero). The startIndex parameter can range from 0 to the length of the string instance.

This method performs a word (case-sensitive and culture-sensitive) search using the current culture. The search begins at startIndex and continues to startIndex + count -1. The character at startIndex + count is not included in the search.

Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. In a culture-sensitive search, if value contains an ignorable character, the result is equivalent to searching with that character removed. If value consists only of one or more ignorable characters, the string.IndexOf(string, int, int) method always returns startIndex, which is the character position at which the search begins. In the following example, the string.IndexOf(string, int, int) method is used to find the position of a soft hyphen (U+00AD) followed by an "m" starting in the third through sixth character positions in two strings. Only one of the strings contains the required substring. In both cases, because the soft hyphen is an ignorable character, the method returns the index of "m" in the string when it performs a culture-sensitive comparison. Note that in the case of the first string, which includes the soft hyphen followed by an "m", the method fails to return the index of the soft hyphen but instead returns the index of the "m".

code reference: System.String.IndexOf#23


Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0,,