다음을 통해 공유


String.Length 속성

이 인스턴스의 문자 수를 가져옵니다.

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public ReadOnly Property Length As Integer
‘사용 방법
Dim instance As String
Dim value As Integer

value = instance.Length
public int Length { get; }
public:
property int Length {
    int get ();
}
/** @property */
public int get_Length ()
public function get Length () : int

속성 값

이 인스턴스의 문자 수입니다.

설명

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

예제

다음 코드 예제에서는 Length 속성을 보여 줍니다.

' Sample for String.Length
Imports System

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)
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'1) The length of 'abcdefg' is 7
'2) The length of 'xyz' is 3
'
// Sample for String.Length
using System;

class Sample 
{
    public static void 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", "xyz".Length);
    }
}
/*
This example produces the following results:
1) The length of 'abcdefg' is 7
2) The length of 'xyz' is 3
*/
// 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 );
}

/*
This example produces the following results:
1) The length of 'abcdefg' is 7
2) The length of 'xyz' is 3
*/
// Sample for String.Length
import System.*;

class Sample
{
    public static void main(String[] args)
    {
        String str = "abcdefg";
        Console.WriteLine("1) The length of '{0}' is {1}", str, 
            (Int32)str.get_Length());
        Console.WriteLine("2) The length of '{0}' is {1}", "xyz", 
            (Int32)"xyz".get_Length());
    } //main
} //Sample
/*
This example produces the following results:
1) The length of 'abcdefg' is 7
2) The length of 'xyz' is 3
*/

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

String 클래스
String 멤버
System 네임스페이스
Int32 구조체