本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语

String.Chars 属性 (Int32)

 

获取当前 Char 对象中位于指定位置的 String 对象。

命名空间:   System
程序集:  mscorlib(位于 mscorlib.dll)

public char this[
	int index
] { get; }

参数

index
Type: System.Int32

当前的字符串中的位置。

属性值

Type: System.Char

位于 index 位置的对象。

Exception Condition
IndexOutOfRangeException

index 大于或等于此对象的长度或小于零。

The index parameter is zero-based.

This property returns the T:System.Char object at the position specified by the index parameter. However, a Unicode character might be represented by more than one T:System.Char. Use the T:System.Globalization.StringInfo class to work with Unicode characters instead of T:System.Char objects. For more information, see the "Char Objects and Unicode Characters" section in the T:System.String class overview.

In C#, the P:System.String.Chars(System.Int32) property is an indexer. In Visual Basic, it is the default property of the T:System.String class. Each T:System.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         

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.            

通用 Windows 平台
自 8 起可用
.NET Framework
自 1.1 起可用
可移植类库
可移植 .NET 平台 中受支持
Silverlight
自 2.0 起可用
Windows Phone Silverlight
自 7.0 起可用
Windows Phone
自 8.1 起可用
返回页首
显示: