String.Chars[Int32] 属性
定义
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.
此属性返回 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. 使用 System.Globalization.StringInfo 类来处理 Unicode 字符,而不是 Char 对象。Use the System.Globalization.StringInfo class to work with Unicode characters instead of Char objects. 有关详细信息,请参阅类概述中的 "Char 对象和 Unicode 字符" 一节 String 。For more information, see the "Char Objects and Unicode Characters" section in the String class overview.
在 c # 中, Chars[] 属性是一个索引器。In C#, the Chars[] 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