Export (0) Print
Expand All

String.IndexOf Method (String)

Reports the zero-based index of the first occurrence of the specified string in this instance.

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

public int IndexOf(
	string value
)

Parameters

value
Type: System.String
The string to seek.

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

ExceptionCondition
ArgumentNullException

value is null.

Index numbering starts from zero.

This method performs a word (case-sensitive and culture-sensitive) search using the current culture. The search begins at the first 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) 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) method in the full .NET Framework. In Silverlight 2 and Silverlight 3, String.IndexOf(String) 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 example uses the IndexOf method to determine the starting position of an animal name in a sentence. It then uses this position to insert an adjective that describes the animal into the sentence.


using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Initialize random number generator to define adjectives to use.
      Random rnd = new Random();

      string animal1 = "fox";
      string animal2 = "dog";
      string[] animal1Adjectives = { "quick", "fast", "speedy", "energetic" };
      string[] animal2Adjectives = { "lazy", "sleeping", "slow-moving", "slothful" };
      string strTarget = String.Format("The {0} jumped over the {1}.", animal1, animal2);
      outputBlock.Text += String.Format("The original string is:\n{0}\n", strTarget);

      // Generate a random number to extract an adjective from the array
      // to describe each animal.
      int animal1AdjectivePosition = rnd.Next(animal1Adjectives.GetLowerBound(0),  
                                              animal1Adjectives.GetUpperBound(0) + 1);
      int animal2AdjectivePosition = rnd.Next(animal1Adjectives.GetLowerBound(0),  
                                              animal1Adjectives.GetUpperBound(0) + 1);                                             
      string animal1Adjective = animal1Adjectives[animal1AdjectivePosition] + " ";
      string animal2Adjective = animal2Adjectives[animal2AdjectivePosition] + " ";

      strTarget = strTarget.Insert(strTarget.IndexOf(animal1), animal1Adjective);
      strTarget = strTarget.Insert(strTarget.IndexOf(animal2), animal2Adjective);

      outputBlock.Text += String.Format("\nThe final string is:\n{0}\n", strTarget);
   }
}
// The output from the example may appear as follows:
//       The original string is:
//       The fox jumped over the dog.
//
//       The final string is:
//       The quick fox jumped over the slothful dog.


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.

Community Additions

ADD
Show:
© 2014 Microsoft