CompareInfo.IndexOf Method (String, Char, Int32, CompareOptions)


Searches for the specified character and returns the zero-based index of the first occurrence within the section of the source string that extends from the specified index to the end of the string using the specified CompareOptions value.

Namespace:   System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

abstract IndexOf : 
        source:string *
        value:char *
        startIndex:int *
        options:CompareOptions -> int
override IndexOf : 
        source:string *
        value:char *
        startIndex:int *
        options:CompareOptions -> int


Type: System.String

The string to search.

Type: System.Char

The character to locate within source.

Type: System.Int32

The zero-based starting index of the search.

Type: System.Globalization.CompareOptions

A value that defines how source and value should be compared. options is either the enumeration value Ordinal, or a bitwise combination of one or more of the following values: IgnoreCase, IgnoreSymbols, IgnoreNonSpace, IgnoreWidth, and IgnoreKanaType.

Return Value

Type: System.Int32

The zero-based index of the first occurrence of value, if found, within the section of source that extends from startIndex to the end of source, using the specified comparison options; otherwise, -1. Returns startIndex if value is an ignorable character.

Exception Condition

source is null.


startIndex is outside the range of valid indexes for source.


options contains an invalid CompareOptions value.

The source string is searched forward starting at startIndex and ending at the end of the string.

The CompareOptions.StringSort value is not valid for this method.

If options does not include the Ordinal value, this overload performs a culture-sensitive search. If the character is a Unicode value representing a precomposed character, such as the ligature "Æ" (U+00C6), it might be considered equivalent to any occurrence of its components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture. If options includes the Ordinal value, this overload performs an ordinal (culture-insensitive) search. A character is considered equivalent to another character only if the Unicode values are the same. Overloads of String.IndexOf that search for a character perform an ordinal search, whereas those that search for a string perform a culture-sensitive search.


When possible, you should call string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. As a general rule, use linguistic options (using the current culture) for comparing strings displayed in the user interface and specify CompareOptions.Ordinal or CompareOptions.OrdinalIgnoreCase for security comparisons.

Notes to Callers:

Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive sort. In a culture-sensitive search, if value is an ignorable character, the result is equivalent to searching with that character removed. In this case, the IndexOf(String, Char, Int32, CompareOptions) method always returns startIndex, the character position at which the search first began. In the following example, the IndexOf(String, Char, Int32, CompareOptions) method is used to find the soft hyphen (U+00AD) after an "n" in two strings. Only one of the strings contains a soft hyphen. In both cases, because the soft hyphen is an ignorable character, a culture-sensitive search returns 1 to indicate that it has found a match at the position of the "n". An ordinal search, however, successfully finds the soft hyphen in one string and reports that it is absent from the second string.

No code example is currently available or this language may not be supported.

The following example determines the indexes of the first and last occurrences of a character or a substring within a portion of a string. Note that IndexOf and LastIndexOf are searching in different portions of the string, even with the same startIndex parameter.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top