String.Chars[Int32] Właściwość

Definicja

Pobiera Char obiekt w określonej pozycji w bieżącym String obiekcie.

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

Parametry

index
Int32

Pozycja w bieżącym ciągu.

Wartość właściwości

Char

Obiekt na pozycji index .

Wyjątki

index jest większa niż lub równa długości tego obiektu lub mniejsza od zera.

Przykłady

W poniższym przykładzie pokazano, jak używać tego indeksatora w rutynowej do weryfikowania ciągu.

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

Uwagi

Parametr index ma wartość zero.

Ta właściwość zwraca Char obiekt w pozycji określonej przez parametr index . Jednak znak Unicode może być reprezentowany przez więcej niż jeden Char znak . Użyj klasy System.Globalization.StringInfo do pracy ze znakami Unicode, a nie Char obiektami. Aby uzyskać więcej informacji, zobacz sekcję "Obiekty Char i znaki Unicode" w String przeglądzie klasy.

W języku C# Chars[] właściwość jest indeksatorem. W Visual Basic jest to domyślna właściwość String klasy . Do Char każdego obiektu w ciągu można uzyskać dostęp przy użyciu kodu takiego jak poniższy.

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

Dotyczy

Zobacz też