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.

Syntax

public int IndexOf (string value, int startIndex)

Parameters

value
The string to seek.
startIndex
The search starting position.

Returns

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.

Exceptions

TypeReason
ArgumentNullException value is a null reference.
ArgumentOutOfRangeException startIndex is greater than the length of the current instance.

Remarks

Index numbering starts from 0. The startIndex parameter can range from 0 to the length of the string instance. If startIndex equals the length of the string instance, the method returns -1.

This method performs a word (case-sensitive and culture-sensitive) search using the current culture. The search begins at the startIndex character position of this instance and continues until the last character position.

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) method always returns startIndex, which is the character position at which the search begins. In the following example, the string.IndexOf(string, int) method is used to find the position of a soft hyphen (U+00AD) followed by an "m" 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. 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#22

Requirements

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