Mono Class Library: System.String Overview | Members

System.String.IndexOf Method

Returns the index of the first occurrence of a specified string in the current instance. [Edit]

public int IndexOf (string value)

Parameters

value
The string for which to search. [Edit]

Returns

A int that indicates the result of the search for value in the current instance as follows:

Return ValueDescription
A zero-based number equal to the index of the start of the first substring in the current instance that is equal to value .value was found starting at the index returned.
-1value was not found.
[Edit]

Exceptions

TypeReason
ArgumentNullExceptionvalue is a null reference. [Edit]

Remarks

The search begins at the first character of the current instance. The search is case-sensitive, culture-sensitive, and the culture (if any) of the current thread is used. [Edit]

Example

The following example demonstrates the string.IndexOf(char) method.

C# Example
using System;
public class StringIndexOf {
 public static void Main() {
 String str = "This is the string";
 Console.WriteLine( "Searching for the index of \"is\" yields {0,2}.", str.IndexOf( "is" ) );
 Console.WriteLine( "Searching for the index of \"Is\" yields {0,2}.", str.IndexOf( "Is" ) );
 Console.WriteLine( "Searching for the index of \"\" yields {0,2}.", str.IndexOf( "" ) );
 }
}

The output is

Searching for the index of "is" yields 2.
Searching for the index of "Is" yields -1.
Searching for the index of "" yields 0.

Requirements

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