Skip to main content
.NET Framework Class Library
StringIndexOf 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)
Syntax
Public Function IndexOf ( _
	value As [%$TOPIC/k8b1470s_en-us_VS_110_1_0_0_0_0%] _
) As [%$TOPIC/k8b1470s_en-us_VS_110_1_0_0_0_1%]
public [%$TOPIC/k8b1470s_en-us_VS_110_1_0_1_0_0%] IndexOf(
	[%$TOPIC/k8b1470s_en-us_VS_110_1_0_1_0_1%] value
)
public:
[%$TOPIC/k8b1470s_en-us_VS_110_1_0_2_0_0%] IndexOf(
	[%$TOPIC/k8b1470s_en-us_VS_110_1_0_2_0_1%]^ value
)
member IndexOf : 
        value:[%$TOPIC/k8b1470s_en-us_VS_110_1_0_3_0_0%] -> [%$TOPIC/k8b1470s_en-us_VS_110_1_0_3_0_1%]
public function IndexOf(
	value : [%$TOPIC/k8b1470s_en-us_VS_110_1_0_4_0_0%]
) : [%$TOPIC/k8b1470s_en-us_VS_110_1_0_4_0_1%]

Parameters

value
Type: SystemString

The string to seek.

Return Value

Type: SystemInt32
The zero-based index position of value if that string is found, or -1 if it is not. If value is StringEmpty, the return value is 0.
Exceptions
ExceptionCondition
ArgumentNullException

value is .

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.

Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. In a culture-sensitive search, if value contains an ignorable character, the result is equivalent to searching with that character removed. If value consists only of one or more ignorable characters, the IndexOf(String) method always returns 0 (zero) to indicate that the match is found at the beginning of the current instance. In the following example, the IndexOf(String) method is used to find three substrings (a soft hyphen (U+00AD), a soft hyphen followed by "n", and a soft hyphen followed by "m") in two strings. Only one of the strings contains a soft hyphen. If the example is run on the .NET Framework 4 or later, in each case, because the soft hyphen is an ignorable character, the result is the same as if the soft hyphen had not been included in value. When searching for a soft hyphen only, the method returns 0 (zero) to indicate that it has found a match at the beginning of the string.

Module Example
   Public Sub Main()
      Dim softHyphen As String = ChrW(&h00AD)
      Dim s1 As String = "ani" + softHyphen + "mal" 
      Dim s2 As String = "animal" 

      ' Find the index of the soft hyphen.
      Console.WriteLine(s1.IndexOf(softHyphen))
      Console.WriteLine(s2.IndexOf(softHyphen))

      ' Find the index of the soft hyphen followed by "n".
      Console.WriteLine(s1.IndexOf(softHyphen + "n"))
      Console.WriteLine(s2.IndexOf(softHyphen + "n"))

      ' Find the index of the soft hyphen followed by "m".
      Console.WriteLine(s1.IndexOf(softHyphen + "m"))
      Console.WriteLine(s2.IndexOf(softHyphen + "m"))
   End Sub 
End Module 
' The example displays the following output  
' if run under the .NET Framework 4 or later: 
'       0 
'       0 
'       1 
'       1 
'       4 
'       3
using System;

public class Example
{
   public static void Main()
   {
      string s1 = "ani\u00ADmal";
      string s2 = "animal";

      // Find the index of the soft hyphen.
      Console.WriteLine(s1.IndexOf("\u00AD"));
      Console.WriteLine(s2.IndexOf("\u00AD"));

      // Find the index of the soft hyphen followed by "n".
      Console.WriteLine(s1.IndexOf("\u00ADn"));
      Console.WriteLine(s2.IndexOf("\u00ADn"));

      // Find the index of the soft hyphen followed by "m".
      Console.WriteLine(s1.IndexOf("\u00ADm"));
      Console.WriteLine(s2.IndexOf("\u00ADm"));
   }
}
// The example displays the following output 
// if run under the .NET Framework 4 or later: 
//       0 
//       0 
//       1 
//       1 
//       4 
//       3

Notes to Callers

As explained in Best Practices for Using Strings in the .NET Framework, we recommend that you avoid calling string comparison methods that substitute default values and 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(String, StringComparison) method overload with a value of StringComparisonCurrentCulture for its comparisonType parameter.

Examples

The following example searches for the "n" in "animal". Because string indexes begin at zero rather than one, the IndexOf(String) method indicates that the "n" is at position 1.

Public Module Example
   Public Sub Main()
      Dim str As String = "animal" 
      Dim toFind As String = "n" 
      Dim index As Integer = str.IndexOf("n")
      Console.WriteLine("Found '{0}' in '{1}' at position {2}",
                        toFind, str, index)
   End Sub 
End Module 
' The example displays the following output: 
'       Found 'n' in 'animal' at position 1
using System;

public class Example
{
   public static void Main()
   {
      String str = "animal";
      String toFind = "n";
      int index = str.IndexOf("n");
      Console.WriteLine("Found '{0}' in '{1}' at position {2}",
                        toFind, str, index);
   }
}
// The example displays the following output: 
//        Found 'n' in 'animal' at position 1
using namespace System;

void main()
{
   String^ str = "animal";
   String^ toFind = "n";
   int index = str->IndexOf("n");
   Console::WriteLine("Found '{0}' in '{1}' at position {2}",
                        toFind, str, index);

}
// The example displays the following output: 
//        Found 'n' in 'animal' at position 1

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 Main()
        Dim animal1 As String = "fox" 
        Dim animal2 As String = "dog" 
        Dim strTarget As String = String.Format("The {0} jumped over the {1}.", 
                                                animal1, animal2)

        Console.WriteLine("The original string is: {0}{1}{0}", 
                          Environment.NewLine, strTarget)

        Console.Write("Enter an adjective (or group of adjectives) " +
                      "to describe the {0}: ==> ", animal1)
        Dim adj1 As String = Console.ReadLine()

        Console.Write("Enter an adjective (or group of adjectives) " + 
                      "to describe the {0}: ==> ", animal2)
        Dim adj2 As String = Console.ReadLine()

        adj1 = adj1.Trim() + " "
        adj2 = adj2.Trim() + " "

        strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1)
        strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2)

        Console.WriteLine("{0}The final string is:{0}{1}", 
                          Environment.NewLine, strTarget)
    End Sub  
End Class  
' Output from the example might appear as follows: 
'       The original string is: 
'       The fox jumped over the dog. 
'        
'       Enter an adjective (or group of adjectives) to describe the fox: ==> bold 
'       Enter an adjective (or group of adjectives) to describe the dog: ==> lazy 
'        
'       The final string is: 
'       The bold fox jumped over the lazy dog.
using System;

public class Example {
    public static void Main() 
    {
        string animal1 = "fox";
        string animal2 = "dog";

        string strTarget = String.Format("The {0} jumped over the {1}.", 
                                         animal1, animal2);

        Console.WriteLine("The original string is:{0}{1}{0}", 
                          Environment.NewLine, strTarget);

        Console.Write("Enter an adjective (or group of adjectives) " +
                      "to describe the {0}: ==> ", animal1);
        string adj1 = Console.ReadLine();

        Console.Write("Enter an adjective (or group of adjectives) " + 
                      "to describe the {0}: ==> ", animal2);    
        string adj2 = Console.ReadLine();

        adj1 = adj1.Trim() + " ";
        adj2 = adj2.Trim() + " ";

        strTarget = strTarget.Insert(strTarget.IndexOf(animal1), adj1);
        strTarget = strTarget.Insert(strTarget.IndexOf(animal2), adj2);

        Console.WriteLine("{0}The final string is:{0}{1}", 
                          Environment.NewLine, strTarget);
    }
}
// Output from the example might appear as follows: 
//       The original string is: 
//       The fox jumped over the dog. 
//        
//       Enter an adjective (or group of adjectives) to describe the fox: ==> bold 
//       Enter an adjective (or group of adjectives) to describe the dog: ==> lazy 
//        
//       The final string is: 
//       The bold fox jumped over the lazy dog.
using namespace System;

int main()
{
   String^ animal1 = "fox";
   String^ animal2 = "dog";
   String^ strTarget = String::Format( "The {0} jumped over the {1}.", animal1, animal2 );
   Console::WriteLine( "The original string is:{0}{1}{0}", Environment::NewLine, strTarget );
   Console::Write( "Enter an adjective (or group of adjectives) to describe the {0}: ==> ", animal1 );
   String^ adj1 = Console::ReadLine();
   Console::Write( "Enter an adjective (or group of adjectives) to describe the {0}: ==> ", animal2 );
   String^ adj2 = Console::ReadLine();
   adj1 = String::Concat( adj1->Trim(), " " );
   adj2 = String::Concat( adj2->Trim(), " " );
   strTarget = strTarget->Insert( strTarget->IndexOf( animal1 ), adj1 );
   strTarget = strTarget->Insert( strTarget->IndexOf( animal2 ), adj2 );
   Console::WriteLine( " {0}The final string is: {0} {1}", Environment::NewLine, strTarget );
}
// Output from the example might appear as follows: 
//       The original string is: 
//       The fox jumped over the dog. 
//        
//       Enter an adjective (or group of adjectives) to describe the fox: ==> bold 
//       Enter an adjective (or group of adjectives) to describe the dog: ==> lazy 
//        
//       The final string is: 
//       The bold fox jumped over the lazy dog.
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library