String.IndexOf Method (String, Int32)

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

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

public int IndexOf(
	string value,
	int startIndex


Type: System.String
The string to seek.
Type: System.Int32
The search starting position.

Return Value

Type: System.Int32
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.


value is null.


startIndex is less than zero or greater than the length of this string.

Index numbering starts from zero. startIndex can range from 0 to one less than 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 the startIndex character position of this instance and continues until the last character position.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

The String.IndexOf method returns incorrect output when the string contains Unicode characters.

Notes to Callers

Starting in Silverlight 4, the behavior of the String.IndexOf(String, Int32) method has changed. In Silverlight 4, it performs a case-sensitive and culture-sensitive comparison using the current culture to find the first occurrence of value after the startIndex position. This conforms to the behavior of the String.IndexOf(String, Int32) method in the full .NET Framework. In Silverlight 2 and Silverlight 3, String.IndexOf(String, Int32) performs an ordinal comparison. If the common language runtime determines that a Silverlight-based application was compiled using either Silverlight 2 or Silverlight 3, it performs an ordinal comparison; otherwise, it performs a culture-sensitive comparison.

The following code example searches for all occurrences of a specified string within a target string.

using System;

public class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)

      string strSource = "This is the string which we will perform the search on";

      outputBlock.Text += String.Format("The search string is:{0}\"{1}\"{0}", "\n", strSource) + "\n";

      string strTarget = "";
      int found = 0;
      int totFinds = 0;

         outputBlock.Text += "Please enter a search value to look for in the above string (hit Enter to exit) ==> ";

         strTarget = Console.ReadLine();

         if (strTarget != "")

            for (int i = 0; i < strSource.Length; i++)

               found = strSource.IndexOf(strTarget, i);

               if (found > 0)
                  i = found;

         outputBlock.Text += String.Format("{0}The search parameter '{1}' was found {2} times.{0}",
                 "\n", strTarget, totFinds) + "\n";

         totFinds = 0;

      } while (true);


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.