共用方式為


String.IndexOf Method (String, Int32, 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 this instance. The search starts at a specified character position and examines a specified number of character positions.

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

Syntax

'Declaration
Public Function IndexOf ( _
    value As String, _
    startIndex As Integer, _
    count As Integer _
) As Integer
public int IndexOf(
    string value,
    int startIndex,
    int count
)

Parameters

  • startIndex
    Type: System.Int32
    The search starting position.
  • count
    Type: System.Int32
    The number of character positions to examine.

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

count or startIndex is negative.

-or-

startIndex is greater than the length of this string.

-or-

count is greater than the length of this string minus startIndex.

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 startIndex and continues to startIndex + count -1. The character at startIndex + count is not included in the search.

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, 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. This conforms to the behavior of the String.IndexOf(String, Int32, Int32) method in the full .NET Framework. In Silverlight 2 and Silverlight 3, String.IndexOf(String, Int32, Int32) performs an ordinal comparison. If the common language runtime determines that a Silverlight-based application was compiled against Silverlight 2 or Silverlight 3, it performs an ordinal comparison; otherwise, it performs a culture-sensitive comparison.

Examples

The following code example finds the index of all occurrences of the string "he" within a substring of another string. Note that the number of characters to be searched must be recalculated for each search iteration.

' Sample for String.IndexOf(String, Int32, Int32)

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
      Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
      Dim str As String = "Now is the time for all good men to come to the aid of their party."
      Dim start As Integer
      Dim at As Integer
      Dim [end] As Integer
      Dim count As Integer

      [end] = str.Length
      start = [end] / 2
      outputBlock.Text &= vbCrLf
      outputBlock.Text &= String.Format("All occurrences of 'he' from position {0} to {1}.", start, [end] - 1) & vbCrLf
      outputBlock.Text &= String.Format("{1}{0}{2}{0}{3}{0}", vbCrLf, br1, br2, str) & vbCrLf
      outputBlock.Text &= "The string 'he' occurs at position(s): "

      count = 0
      at = 0
      While start <= [end] AndAlso at > -1
         ' start+count must be a position within -str-.
         count = [end] - start
         at = str.IndexOf("he", start, count)
         If at = -1 Then
            Exit While
         End If
         outputBlock.Text &= String.Format("{0} ", at)
         start = at + 1
      End While
      outputBlock.Text &= vbCrLf
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'All occurrences of 'he' from position 33 to 66.
'0----+----1----+----2----+----3----+----4----+----5----+----6----+-
'0123456789012345678901234567890123456789012345678901234567890123456
'Now is the time for all good men to come to the aid of their party.
'
'The string 'he' occurs at position(s): 45 56
'
'
// Sample for String.IndexOf(String, Int32, Int32)
using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
      string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
      string str = "Now is the time for all good men to come to the aid of their party.";
      int start;
      int at;
      int end;
      int count;

      end = str.Length;
      start = end / 2;
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("All occurrences of 'he' from position {0} to {1}.", start, end - 1) + "\n";
      outputBlock.Text += String.Format("{1}{0}{2}{0}{3}{0}", "\n", br1, br2, str) + "\n";
      outputBlock.Text += "The string 'he' occurs at position(s): ";

      count = 0;
      at = 0;
      while ((start <= end) && (at > -1))
      {
         // start+count must be a position within -str-.
         count = end - start;
         at = str.IndexOf("he", start, count);
         if (at == -1) break;
         outputBlock.Text += String.Format("{0} ", at);
         start = at + 1;
      }
      outputBlock.Text += "\n";
   }
}
/*
This example produces the following results:

All occurrences of 'he' from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.

The string 'he' occurs at position(s): 45 56

*/

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.