다음을 통해 공유


String.Chars[Int32] 속성

정의

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

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

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

속성 값

위치에 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.
open System

printf "Type a string: "
let myString = stdin.ReadLine()
for i = 0 to myString.Length - 1 do
    if Uri.IsHexDigit myString[i] then
        printfn $"{myString[i]} is a hexadecimal digit."
    else
        printfn $"{myString[i]} is not a hexadecimal digit."
// 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[Int32] 속성은 인덱서입니다. 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
let str1 = "Test"
for i = 0 to str1.Length - 1 do
    printf $"{str1[i]} "
// 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

적용 대상

추가 정보