Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.IndexOf Method (String, Int32, Int32)

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

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

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

Parameters

value
Type: System.String
The string to seek.
startIndex
Type: System.Int32
The search starting position.
count
Type: System.Int32
The number of character positions to examine.

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.

ExceptionCondition
ArgumentNullException

value is null.

ArgumentOutOfRangeException

count or startIndex is negative.

-or-

startIndex is greater than the length of this string.

-or-

count is greater than the length of this string minus startIndex.

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 startIndex and continues to startIndex + count -1. The character at startIndex + count is not included in the search.

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, 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. This conforms to the behavior of the String.IndexOf(String, Int32, Int32) method in the full .NET Framework. In Silverlight 2 and Silverlight 3, String.IndexOf(String, Int32, Int32) performs an ordinal comparison. If the common language runtime determines that a Silverlight-based application was compiled against Silverlight 2 or Silverlight 3, it performs an ordinal comparison; otherwise, it performs a culture-sensitive comparison.

The following code example finds the index of all occurrences of the string "he" within a substring of another string. Note that the number of characters to be searched must be recalculated for each search iteration.


// Sample for String.IndexOf(String, Int32, Int32)
using System;

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

      string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
      string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
      string str = "Now is the time for all good men to come to the aid of their party.";
      int start;
      int at;
      int end;
      int count;

      end = str.Length;
      start = end / 2;
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("All occurrences of 'he' from position {0} to {1}.", start, end - 1) + "\n";
      outputBlock.Text += String.Format("{1}{0}{2}{0}{3}{0}", "\n", br1, br2, str) + "\n";
      outputBlock.Text += "The string 'he' occurs at position(s): ";

      count = 0;
      at = 0;
      while ((start <= end) && (at > -1))
      {
         // start+count must be a position within -str-.
         count = end - start;
         at = str.IndexOf("he", start, count);
         if (at == -1) break;
         outputBlock.Text += String.Format("{0} ", at);
         start = at + 1;
      }
      outputBlock.Text += "\n";
   }
}
/*
This example produces the following results:

All occurrences of 'he' from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.

The string 'he' occurs at position(s): 45 56

*/


Silverlight

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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft