String.IndexOf Method (String, Int32)
Microsoft Silverlight will reach end of support after October 2021. Learn more.
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)
Syntax
'Declaration
Public Function IndexOf ( _
value As String, _
startIndex As Integer _
) As Integer
public int IndexOf(
string value,
int startIndex
)
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.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | value is nulla null reference (Nothing in Visual Basic). |
ArgumentOutOfRangeException | startIndex is less than zero or greater than the length of this string. |
Remarks
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
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.
Examples
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
using System;
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
string strSource = "This is the string which we will perform the search on";
outputBlock.Text += String.Format("The search string is:{0}\"{1}\"{0}", "\n", strSource) + "\n";
string strTarget = "";
int found = 0;
int totFinds = 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 != "")
{
for (int i = 0; i < strSource.Length; i++)
{
found = strSource.IndexOf(strTarget, i);
if (found > 0)
{
totFinds++;
i = found;
}
else
break;
}
}
else
return;
outputBlock.Text += String.Format("{0}The search parameter '{1}' was found {2} times.{0}",
"\n", strTarget, totFinds) + "\n";
totFinds = 0;
} while (true);
}
}
Version Information
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
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
See Also