String.Chars[Int32] 属性

定义

获取当前 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

当前的字符串中的位置。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.

此属性返回 index 参数所指定位置处的 Char 对象。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. 使用 System.Globalization.StringInfo 类来处理 Unicode 字符,而不是 Char 对象。Use the System.Globalization.StringInfo class to work with Unicode characters instead of Char objects. 有关详细信息,请参阅 String 类概述中的 "Char 对象和 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         

适用于

另请参阅