String.IndexOf Method (String)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

Syntax

Public Function IndexOf ( _
    value As String _
) As Integer
public int IndexOf(
    string value
)

Parameters

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.

Exceptions

Exception Condition
ArgumentNullException

value is nullNothingnullptra null reference (Nothing in Visual Basic).

Remarks

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.

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 find the first index of a substring within a string instance by using the comparison rules of the current culture, call the IndexOf method overload with a value of StringComparison..::.CurrentCulture for its comparisonType parameter.

Examples

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.

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Initialize random number generator to define adjectives to use.
      Dim rnd As New Random()

      Dim animal1 As String = "fox"
      Dim animal2 As String = "dog"
      Dim animal1Adjectives() As String = { "quick", "fast", "speedy", "energetic" }
      Dim animal2Adjectives() As String = { "lazy", "sleeping", "slow-moving", "slothful" }
      Dim strTarget As String = String.Format("The {0} jumped over the {1}.", animal1, animal2)
      outputBlock.Text &= String.Format("The original string is:{0}{1}{0}", vbCrLf, strTarget)

      ' Generate a random number to extract an adjective from the array
      ' to describe each animal.
      Dim animal1AdjectivePosition As Integer = rnd.Next(animal1Adjectives.GetLowerBound(0), _ 
                                              animal1Adjectives.GetUpperBound(0) + 1)
      Dim animal2AdjectivePosition As Integer = rnd.Next(animal1Adjectives.GetLowerBound(0), _ 
                                              animal1Adjectives.GetUpperBound(0) + 1)                                              
      Dim animal1Adjective As String = animal1Adjectives(animal1AdjectivePosition) + " "
      Dim animal2Adjective As String = animal2Adjectives(animal2AdjectivePosition) + " "

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

      outputBlock.Text &= String.Format("{0}The final string is:{0}{1}{0}", vbCrLf, strTarget)
   End Sub 
End Class 
' 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.
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.

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

String Class

IndexOf Overload

System Namespace

Int32

CultureInfo

IndexOfAny

LastIndexOf

LastIndexOfAny