String.IndexOf Method (String, Int32, Int32)

July 28, 2014

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 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.

Notes to Callers

We recommend that you avoid calling string comparison methods that substitute default values. Instead, call methods that require parameters to be explicitly specified. To use the comparison rules of the current culture to perform this operation, call the IndexOf method overload with a value of StringComparison.CurrentCulture for its comparisonType parameter.

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

*/


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft