String.Chars[Int32] String.Chars[Int32] String.Chars[Int32] String.Chars[Int32] Property

定義

取得目前 Char 物件中指定位置的 String 物件。Gets the Char object at a specified position in the current String object.

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

參數

index
Int32 Int32 Int32 Int32

目前字串中的位置。A position in the current string.

屬性值

index 位置的物件。The object at position index.

例外狀況

index 大於或等於此物件的長度或小於零。index is greater than or equal to the length of this object or less than zero.

範例

下列範例會示範如何在常式中使用此索引子來驗證字串。The following example demonstrates how you can use this indexer in a routine to validate a string.

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.         

備註

index參數是以零為基底。The index parameter is zero-based.

這個屬性Char會傳回index參數所指定位置的物件。This property returns the Char object at the position specified by the index parameter. 不過,Unicode 字元可能會由一個Char以上的來表示。However, a Unicode character might be represented by more than one Char. 使用類別來處理 Unicode 字元, Char而不是物件。 System.Globalization.StringInfoUse the System.Globalization.StringInfo class to work with Unicode characters instead of Char objects. 如需詳細資訊,請參閱String類別總覽中的「字元物件和 Unicode 字元」一節。For more information, see the "Char Objects and Unicode Characters" section in the String class overview.

在C#中, Chars[Int32]屬性是索引子。In C#, the Chars[Int32] property is an indexer. 在 Visual Basic 中,這是String類別的預設屬性。In Visual Basic, it is the default property of the String class. Char可以使用如下的程式碼來存取字串中的每個物件。Each Char object in the string can be accessed by using code such as the following.

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         

適用於

另請參閱