String.Chars[Int32] 속성

정의

현재 Char 개체에서 지정된 문자 위치에 있는 String 개체를 가져옵니다.

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

현재 문자열의 위치입니다.

속성 값

Char

index 위치에 있는 개체입니다.

예외

index가 이 개체의 길이보다 크거나 같거나 0보다 작습니다.

예제

다음 예제에서는 루틴에서이 인덱서를 사용 하 여 문자열의 유효성을 검사 하는 방법을 보여 줍니다.

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매개 변수는 0부터 시작 합니다.

이 속성은 Char 매개 변수로 지정 된 위치에 있는 개체를 반환 합니다 index . 그러나 유니코드 문자는 둘 이상의로 나타낼 수 있습니다 Char . System.Globalization.StringInfo개체 대신 유니코드 문자로 작업 하려면 클래스를 사용 Char 합니다. 자세한 내용은 클래스 개요의 "Char 개체 및 유니코드 문자" 섹션을 참조 하세요 String .

C #에서 Chars[] 속성은 인덱서입니다. Visual Basic에서 클래스의 기본 속성입니다 String . Char문자열의 각 개체에는 다음과 같은 코드를 사용 하 여 액세스할 수 있습니다.

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

적용 대상

추가 정보