IndexOf Method (String)

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


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.

Exception Condition

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.

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, theIndexOf(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.

using System;

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

      // Find the index of the soft hyphen.

      // Find the index of the soft hyphen followed by "n".

      // Find the index of the soft hyphen followed by "m".
// 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 StringComparison.CurrentCulture for its comparisonType parameter.

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.

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

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;
using System.IO;
using System.Xml;

public class Sample
  public static void Main()
    // Create the XmlDocument.
    XmlDocument doc = new XmlDocument();

    // Create a document fragment.
    XmlDocumentFragment docFrag = doc.CreateDocumentFragment();

    // Display the owner document of the document fragment.

    // Add nodes to the document fragment. Notice that the
    // new element is created using the owner document of 
    // the document fragment.
    XmlElement elem = doc.CreateElement("item");
    elem.InnerText = "widget";

    Console.WriteLine("Display the document fragment...");

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft