.NET Framework-Klassenbibliothek
String..::.IndexOf-Methode (Char, Int32, Int32)

Aktualisiert: November 2007

Gibt den Index des ersten Vorkommens des angegebenen Zeichens in dieser Instanz an. Die Suche beginnt an einer angegebenen Zeichenposition, und es wird eine angegebene Anzahl von Zeichenpositionen überprüft.

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

Visual Basic (Deklaration)
Public Function IndexOf ( _
    value As Char, _
    startIndex As Integer, _
    count As Integer _
) As Integer
Visual Basic (Verwendung)
Dim instance As String
Dim value As Char
Dim startIndex As Integer
Dim count As Integer
Dim returnValue As Integer

returnValue = instance.IndexOf(value, _
    startIndex, count)
C#
public int IndexOf(
    char value,
    int startIndex,
    int count
)
VisualC++
public:
int IndexOf(
    wchar_t value, 
    int startIndex, 
    int count
)
J#
public int IndexOf(
    char value,
    int startIndex,
    int count
)
Jscript
public function IndexOf(
    value : char, 
    startIndex : int, 
    count : int
) : int

Parameter

value
Typ: System..::.Char
Ein zu suchendes Unicode-Zeichen.
startIndex
Typ: System..::.Int32
Die Anfangsposition der Suche.
count
Typ: System..::.Int32
Die Anzahl der zu überprüfenden Zeichenpositionen.

Rückgabewert

Typ: System..::.Int32
Die nullbasierte Indexposition von value, wenn dieses Zeichen gefunden wurde, andernfalls -1.
Ausnahmen

AusnahmeBedingung
ArgumentOutOfRangeException

count oder startIndex ist negativ.

– oder –

count + startIndex gibt eine Position hinter dem Ende dieser Instanz an.

Hinweise

Die Suche beginnt bei startIndex und endet bei startIndex + count -1. Das Zeichen bei startIndex + count ist nicht in der Suche enthalten.

Indexnummerierung beginnt mit 0 (null). Der startIndex-Parameter kann zwischen 0 und eins kleiner als die Länge der Zeichenfolgeninstanz liegen.

Bei der Suche nach value wird die Groß- und Kleinschreibung berücksichtigt.

Bei dieser Methode wird eine (kulturunabhängige) Suche auf Basis von Ordnungszahlen durchgeführt, wobei ein Zeichen nur dann als Äquivalent eines anderen Zeichens betrachtet wird, wenn die skalaren Unicode-Werte gleich sind. Für die Durchführung einer kulturabhängigen Suche verwenden Sie die CompareInfo..::.IndexOf-Methode, bei der ein skalarer Unicode-Wert, der ein zusammengesetztes Zeichen wie die Ligatur "Æ" (U+00C6) darstellt, je nach Kultur als Äquivalent für jedes Auftreten der Teile des Zeichens in der richtigen Reihenfolge betrachtet werden kann, beispielsweise "AE" (U+0041, U+0045).

Beispiele

Im folgenden Codebeispiel wird die IndexOf-Methode veranschaulicht.

Visual Basic
' Example for the String.IndexOf( Char, Integer, Integer ) method.
Imports System
Imports Microsoft.VisualBasic

Module IndexOfCII

    Sub Main()
        Dim br1 As String = _
            "0----+----1----+----2----+----3----+----" & _
            "4----+----5----+----6----+----7"
        Dim br2 As String = _
            "0123456789012345678901234567890123456789" & _
            "0123456789012345678901234567890"
        Dim str As String = _
            "ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi " & _
            "ABCDEFGHI abcdefghi ABCDEFGHI"

        Console.WriteLine( _
            "This example of String.IndexOf( Char, Integer, Integer )" & _
            vbCrLf & "generates the following output." )
        Console.WriteLine( _
            "{0}{1}{0}{2}{0}{3}{0}", _
            Environment.NewLine, br1, br2, str)

        FindAllChar("A"c, str)
        FindAllChar("a"c, str)
        FindAllChar("I"c, str)
        FindAllChar("i"c, str)
        FindAllChar("@"c, str)
        FindAllChar(" "c, str)
    End Sub 'Main

    Sub FindAllChar(target As Char, searched As String)

        Console.Write( _
            "The character ""{0}"" occurs at position(s): ", target)

        Dim startIndex As Integer = - 1
        Dim hitCount As Integer = 0

        ' Search for all occurrences of the target.
        While True
            startIndex = searched.IndexOf( _
                target, startIndex + 1, _
                searched.Length - startIndex - 1)

            ' Exit the loop if the target is not found.
            If startIndex < 0 Then
                Exit While
            End If 

            Console.Write("{0}, ", startIndex)
            hitCount += 1
        End While

        Console.WriteLine("occurrences: {0}", hitCount)

    End Sub 'FindAllChar
End Module 'IndexOfCII

' This example of String.IndexOf( Char, Integer, Integer )
' generates the following output.
' 
' 0----+----1----+----2----+----3----+----4----+----5----+----6----+----7
' 01234567890123456789012345678901234567890123456789012345678901234567890
' ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi ABCDEFGHI
' 
' The character "A" occurs at position(s): 0, 20, 40, 60, occurrences: 4
' The character "a" occurs at position(s): 10, 30, 50, occurrences: 3
' The character "I" occurs at position(s): 8, 28, 48, 68, occurrences: 4
' The character "i" occurs at position(s): 18, 38, 58, occurrences: 3
' The character "@" occurs at position(s): occurrences: 0
' The character " " occurs at position(s): 9, 19, 29, 39, 49, 59, occurrences: 6
C#
// Example for the String.IndexOf( char, int, int ) method.
using System;

class IndexOfCII 
{
    public static void Main() 
    {
        string br1 = 
            "0----+----1----+----2----+----3----+----" +
            "4----+----5----+----6----+----7";
        string br2 = 
            "0123456789012345678901234567890123456789" +
            "0123456789012345678901234567890";
        string str = 
            "ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi " +
            "ABCDEFGHI abcdefghi ABCDEFGHI";

        Console.WriteLine( 
            "This example of String.IndexOf( char, int, int )\n" +
            "generates the following output." );
        Console.WriteLine( 
            "{0}{1}{0}{2}{0}{3}{0}", 
            Environment.NewLine, br1, br2, str );

        FindAllChar( 'A', str );
        FindAllChar( 'a', str );
        FindAllChar( 'I', str );
        FindAllChar( 'i', str );
        FindAllChar( '@', str );
        FindAllChar( ' ', str );
    }

    static void FindAllChar( Char target, String searched )
    {
        Console.Write( 
            "The character '{0}' occurs at position(s): ", 
            target );

        int     startIndex = -1;
        int     hitCount = 0;

        // Search for all occurrences of the target.
        while( true )
        {
            startIndex = searched.IndexOf( 
                target, startIndex + 1, 
                searched.Length - startIndex - 1 );

            // Exit the loop if the target is not found.
            if( startIndex < 0 )
                break;

            Console.Write( "{0}, ", startIndex );
            hitCount++;
        }

        Console.WriteLine( "occurrences: {0}", hitCount );
    }
}

/*
This example of String.IndexOf( char, int, int )
generates the following output.

0----+----1----+----2----+----3----+----4----+----5----+----6----+----7
01234567890123456789012345678901234567890123456789012345678901234567890
ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi ABCDEFGHI

The character 'A' occurs at position(s): 0, 20, 40, 60, occurrences: 4
The character 'a' occurs at position(s): 10, 30, 50, occurrences: 3
The character 'I' occurs at position(s): 8, 28, 48, 68, occurrences: 4
The character 'i' occurs at position(s): 18, 38, 58, occurrences: 3
The character '@' occurs at position(s): occurrences: 0
The character ' ' occurs at position(s): 9, 19, 29, 39, 49, 59, occurrences: 6
*/
VisualC++
// Example for the String::IndexOf( Char, int, int ) method.
using namespace System;
void FindAllChar( Char target, String^ searched )
{
   Console::Write( "The character '{0}' occurs at position(s): ", target );
   int startIndex = -1;
   int hitCount = 0;

   // Search for all occurrences of the target.
   while ( true )
   {
      startIndex = searched->IndexOf( target, startIndex + 1, searched->Length - startIndex - 1 );

      // Exit the loop if the target is not found.
      if ( startIndex < 0 )
            break;

      Console::Write( "{0}, ", startIndex );
      hitCount++;
   }

   Console::WriteLine( "occurrences: {0}", hitCount );
}

int main()
{
   String^ br1 = "0----+----1----+----2----+----3----+----"
   "4----+----5----+----6----+----7";
   String^ br2 = "0123456789012345678901234567890123456789"
   "0123456789012345678901234567890";
   String^ str = "ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi "
   "ABCDEFGHI abcdefghi ABCDEFGHI";
   Console::WriteLine( "This example of String::IndexOf( Char, int, int )\n"
   "generates the following output." );
   Console::WriteLine( "{0}{1}{0}{2}{0}{3}{0}", Environment::NewLine, br1, br2, str );
   FindAllChar( 'A', str );
   FindAllChar( 'a', str );
   FindAllChar( 'I', str );
   FindAllChar( 'i', str );
   FindAllChar( '@', str );
   FindAllChar( ' ', str );
}

/*
This example of String::IndexOf( Char, int, int )
generates the following output.

0----+----1----+----2----+----3----+----4----+----5----+----6----+----7
01234567890123456789012345678901234567890123456789012345678901234567890
ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi ABCDEFGHI

The character 'A' occurs at position(s): 0, 20, 40, 60, occurrences: 4
The character 'a' occurs at position(s): 10, 30, 50, occurrences: 3
The character 'I' occurs at position(s): 8, 28, 48, 68, occurrences: 4
The character 'i' occurs at position(s): 18, 38, 58, occurrences: 3
The character '@' occurs at position(s): occurrences: 0
The character ' ' occurs at position(s): 9, 19, 29, 39, 49, 59, occurrences: 6
*/
J#
// Example for the String.IndexOf( char, int, int ) method.
import System.*;

class IndexOfCII
{
    public static void main(String[] args)
    {
        String br1 = "0----+----1----+----2----+----3----+----" 
            + "4----+----5----+----6----+----7";
        String br2 = "0123456789012345678901234567890123456789" 
            + "0123456789012345678901234567890";
        String str = "ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi "
            + "ABCDEFGHI abcdefghi ABCDEFGHI";
        Console.WriteLine("This example of "
            + "String.IndexOf( char, int, int )\n" 
            + "generates the following output.");
        Console.WriteLine("{0}{1}{0}{2}{0}{3}{0}", 
            new Object[] { Environment.get_NewLine(), br1, br2, str });
        FindAllChar('A', str);
        FindAllChar('a', str);
        FindAllChar('I', str);
        FindAllChar('i', str);
        FindAllChar('@', str);
        FindAllChar(' ', str);
    } //main

    static void FindAllChar(char target, String searched)
    {
        Console.Write("The character '{0}' occurs at position(s): ", 
            String.valueOf(target));
        int startIndex = -1;
        int hitCount = 0;

        // Search for all occurrences of the target.
        while(true) {
            startIndex = searched.IndexOf(target, startIndex + 1, 
                searched.get_Length() - startIndex - 1);

            // Exit the loop if the target is not found.
            if (startIndex < 0) {
                break;
            }     
            Console.Write("{0}, ", String.valueOf(startIndex));
            hitCount++;
        }
        Console.WriteLine("occurrences: {0}", String.valueOf(hitCount));
    } //FindAllChar
} //IndexOfCII
/*
This example of String.IndexOf( char, int, int )
generates the following output.

0----+----1----+----2----+----3----+----4----+----5----+----6----+----7
01234567890123456789012345678901234567890123456789012345678901234567890
ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi ABCDEFGHI abcdefghi ABCDEFGHI

The character 'A' occurs at position(s): 0, 20, 40, 60, occurrences: 4
The character 'a' occurs at position(s): 10, 30, 50, occurrences: 3
The character 'I' occurs at position(s): 8, 28, 48, 68, occurrences: 4
The character 'i' occurs at position(s): 18, 38, 58, occurrences: 3
The character '@' occurs at position(s): occurrences: 0
The character ' ' occurs at position(s): 9, 19, 29, 39, 49, 59, occurrences: 6
*/
Plattformen

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile für Smartphone, Windows Mobile für Pocket PC, Xbox 360

.NET Framework und .NET Compact Framework unterstützen nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.
Versionsinformationen

.NET Framework

Unterstützt in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 3.5, 2.0, 1.0

XNA Framework

Unterstützt in: 2.0, 1.0
Siehe auch

Referenz

Tags :


Page view tracker