Export (0) Print
Expand All

String.IndexOf Method (String, Int32)

Reports the zero-based index of the first occurrence of the specified string in this instance. The search starts at a specified character position.

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

'Declaration
Public Function IndexOf ( _
	value As String, _
	startIndex As Integer _
) As Integer

Parameters

value
Type: System.String
The string to seek.
startIndex
Type: System.Int32
The search starting position.

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

ArgumentOutOfRangeException

startIndex is less than zero or greater than the length of this string.

Index numbering starts from zero. startIndex can range from 0 to one less than 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 the startIndex 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, Int32) 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 after the startIndex position. This conforms to the behavior of the String.IndexOf(String, Int32) method in the full .NET Framework. In Silverlight 2 and Silverlight 3, String.IndexOf(String, Int32) 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 code example searches for all occurrences of a specified string within a target string.



Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim strSource As String = "This is the string which we will perform the search on"

      outputBlock.Text &= String.Format("The search string is:{0}{1}{0}", vbCrLf, strSource) & vbCrLf
      Dim strTarget As String = ""
      Dim found As Integer = 0
      Dim totFinds As Integer = 0

      Do
         outputBlock.Text &= "Please enter a search value to look for in the above string (hit Enter to exit) ==> "

         strTarget = Console.ReadLine()
         If strTarget <> "" Then
            Dim i As Integer


            For i = 0 To strSource.Length - 1

               found = strSource.IndexOf(strTarget, i)
               If found > 0 Then

                  totFinds += 1
                  i = found
               Else
                  Exit For
               End If
            Next i
         Else
            Return

         End If
         outputBlock.Text &= String.Format("{0}The search parameter '{1}' was found {2} times.{0}", vbCrLf, strTarget, totFinds) & vbCrLf

         totFinds = 0

      Loop While True
   End Sub 'Main
End Class 'IndexOfTest


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