String.Length Property

Definition

현재 String 개체의 문자 수를 가져옵니다.Gets the number of characters in the current String object.

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

Property Value

Int32

현재 문자열의 문자 수를 가져옵니다.The number of characters in the current string.

Examples

다음 예제는 Length 속성입니다.The following example demonstrates the Length property.

// 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
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

Remarks

Length 속성은 유니코드 문자 수가 아니라이 인스턴스에 있는 Char 개체의 수를 반환 합니다.The Length property returns the number of Char objects in this instance, not the number of Unicode characters. 유니코드 문자를 두 개 이상의 Char로 나타낼 수 있기 때문입니다.The reason is that a Unicode character might be represented by more than one Char. Char대신 각 유니코드 문자로 작업 하려면 System.Globalization.StringInfo 클래스를 사용 합니다.Use the System.Globalization.StringInfo class to work with each Unicode character instead of each Char.

C 및 c + +와 같은 일부 언어에서 null 문자를 문자열의 끝을 나타냅니다.In some languages, such as C and C++, a null character indicates the end of a string. .NET에서 문자열에 null 문자를 포함할 수 있습니다.In .NET, a null character can be embedded in a string. 문자열에서 하나 이상의 null 문자를 포함 하는 경우 전체 문자열의 길이가 포함 됩니다.When a string includes one or more null characters, they are included in the length of the total string. 예를 들어 다음 문자열을 부분 문자열 "abc"와 "def" null 문자로 구분 됩니다에서.For example, in the following string, the substrings "abc" and "def" are separated by a null character. Length 속성은 7을 반환 하며,이 값은 6 개의 영문자 및 null 문자를 포함 함을 나타냅니다.The Length property returns 7, which indicates that it includes the six alphabetic characters as well as the null character.

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
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

Applies to

See also