String.Length 속성

정의

현재 String 개체의 문자 수를 가져옵니다.

public:
 property int Length { int get(); };
public int Length { get; }
member this.Length : int
Public ReadOnly Property Length As Integer

속성 값

현재 문자열의 문자 수를 가져옵니다.

예제

다음 예제는 Length 속성입니다.

// Sample for String::Length
using namespace System;
int main()
{
   String^ str = "abcdefg";
   Console::WriteLine( "1) The length of '{0}' is {1}", str, str->Length );
   Console::WriteLine( "2) The length of '{0}' is {1}", "xyz", ((String^)"xyz")->Length );
   int length = str->Length;
   Console::WriteLine( "1) The length of '{0}' is {1}", str, length );
}

/*
This example displays the following output:
   1) The length of 'abcdefg' is 7
   2) The length of 'xyz' is 3
   3) The length of 'abcdefg' is 7
*/
string str = "abcdefg";
Console.WriteLine("1) The length of '{0}' is {1}", str, str.Length);
Console.WriteLine("2) The length of '{0}' is {1}", "xyz", "xyz".Length);

int length = str.Length;
Console.WriteLine("3) The length of '{0}' is {1}", str, length);

// This example displays the following output:
//    1) The length of 'abcdefg' is 7
//    2) The length of 'xyz' is 3
//    3) The length of 'abcdefg' is 7
let str = "abcdefg"
printfn $"1) The length of '{str}' is {str.Length}"
printfn $"""2) The length of '{"xyz"}' is {"xyz".Length}"""

let length = str.Length
printfn $"3) The length of '{str}' is {length}"

// This example displays the following output:
//    1) The length of 'abcdefg' is 7
//    2) The length of 'xyz' is 3
//    3) The length of 'abcdefg' is 7
Class Sample
   Public Shared Sub Main()
      Dim str As String = "abcdefg"
      Console.WriteLine("1) The length of '{0}' is {1}", str, str.Length)
      Console.WriteLine("2) The length of '{0}' is {1}", "xyz", "xyz".Length)
      Dim length As Integer = str.Length
      Console.WriteLine("1) The length of '{0}' is {1}", str, length)
   End Sub
End Class
'
'This example displays the following output:
'    1) The length of 'abcdefg' is 7
'    2) The length of 'xyz' is 3
'    3) The length of 'abcdefg' is 7

설명

속성은 Length 유니코드 문자 수가 Char 아닌 이 인스턴스의 개체 수를 반환합니다. 그 이유는 유니코드 문자가 둘 Char이상의 로 표현될 수 있기 때문입니다. 클래스를 System.Globalization.StringInfo 사용하여 각 Char대신 각 유니코드 문자로 작업합니다.

C 및 C++와 같은 일부 언어에서는 null 문자가 문자열의 끝을 나타냅니다. .NET에서 null 문자를 문자열에 포함할 수 있습니다. 문자열에 Null 문자가 하나 이상 포함된 경우 총 문자열의 길이에 포함됩니다. 예를 들어 다음 문자열에서 부분 문자열 "abc" 및 "def"는 null 문자로 구분됩니다. 속성은 Length 7을 반환합니다. 이 값은 6개의 알파벳 문자와 null 문자를 포함했음을 나타냅니다.

using namespace System;
using namespace System::Text;

void main()
{
   String^ characters = "abc" + L'0' + "def";
   Console::WriteLine(characters->Length);        // Displays 7
}
string characters = "abc\u0000def";
Console.WriteLine(characters.Length); // Displays 7
let characters = "abc\u0000def"
printfn $"{characters.Length}" // Displays 7
Imports System.Text

Module Example

   Public Sub Main()
      Dim characters As String = "abc" + ChrW(0) + "def"
      Console.WriteLine(characters.Length)       ' Displays 7
   End Sub
End Module

적용 대상

추가 정보