Freigeben über


String.Chars[Int32] Eigenschaft

Definition

Ruft das Char Objekt an einer angegebenen Position im aktuellen String Objekt ab.

public:
 property char default[int] { char get(int index); };
public char this[int index] { get; }
member this.Chars(int) : char
Default Public ReadOnly Property Chars(index As Integer) As Char

Parameter

index
Int32

Eine Position in der aktuellen Zeichenfolge.

Eigenschaftswert

Das Objekt an der Position index.

Ausnahmen

index ist größer oder gleich der Länge dieses Objekts oder kleiner als Null.

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie Sie diesen Indexer in einer Routine verwenden können, um eine Zeichenfolge zu überprüfen.

Console.Write("Type a string : ");
string myString = Console.ReadLine();
for (int i = 0; i < myString.Length; i ++)
   if(Uri.IsHexDigit(myString[i]))
      Console.WriteLine("{0} is a hexadecimal digit.", myString[i]);
   else
      Console.WriteLine("{0} is not a hexadecimal digit.", myString[i]);
// The example produces output like the following:
//    Type a string : 3f5EaZ
//    3 is a hexadecimal digit.
//    f is a hexadecimal digit.
//    5 is a hexadecimal digit.
//    E is a hexadecimal digit.
//    a is a hexadecimal digit.
//    Z is not a hexadecimal digit.
open System

printf "Type a string: "
let myString = stdin.ReadLine()
for i = 0 to myString.Length - 1 do
    if Uri.IsHexDigit myString[i] then
        printfn $"{myString[i]} is a hexadecimal digit."
    else
        printfn $"{myString[i]} is not a hexadecimal digit."
// The example produces output like the following:
//    Type a string: 3f5EaZ
//    3 is a hexadecimal digit.
//    f is a hexadecimal digit.
//    5 is a hexadecimal digit.
//    E is a hexadecimal digit.
//    a is a hexadecimal digit.
//    Z is not a hexadecimal digit.
Console.Write("Type a string : ")
Dim myString As String = Console.ReadLine()
Dim i As Integer
For i = 0 To myString.Length - 1
   If Uri.IsHexDigit(myString.Chars(i)) Then
      Console.WriteLine("{0} is a hexadecimal digit.", myString.Chars(i))
   Else
      Console.WriteLine("{0} is not a hexadecimal digit.", myString.Chars(i))
   End If 
Next
' The example produces output like the following:
'    Type a string : 3f5EaZ
'    3 is a hexadecimal digit.
'    f is a hexadecimal digit.
'    5 is a hexadecimal digit.
'    E is a hexadecimal digit.
'    a is a hexadecimal digit.
'    Z is not a hexadecimal digit.

Hinweise

Der index Parameter ist nullbasiert.

Diese Eigenschaft gibt das Char Objekt an der durch den index Parameter angegebenen Position zurück. Ein Unicode-Zeichen kann jedoch durch mehrere Chardargestellt werden. Verwenden Sie die System.Globalization.StringInfo Klasse, um mit Unicode-Zeichen anstelle von Char Objekten zu arbeiten. Weitere Informationen finden Sie im Abschnitt "Zeichenobjekte und Unicode-Zeichen" in der String Klassenübersicht.

In C# ist die Chars[Int32] Eigenschaft ein Indexer. In Visual Basic ist dies die Standardeigenschaft der String Klasse. Auf jedes Char Objekt in der Zeichenfolge kann mithilfe von Code wie dem folgenden zugegriffen werden.

string str1 = "Test";
for (int ctr = 0; ctr <= str1.Length - 1; ctr++ )
   Console.Write("{0} ", str1[ctr]);
// The example displays the following output:
//      T e s t
let str1 = "Test"
for i = 0 to str1.Length - 1 do
    printf $"{str1[i]} "
// The example displays the following output:
//      T e s t
Dim str1 As String = "Test"
For ctr As Integer = 0 to str1.Length - 1
   Console.Write("{0} ", str1(ctr))
Next   
' The example displays the following output:
'      T e s t

Gilt für:

Weitere Informationen