Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System
String Class
String Properties
 Length Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
String.Length Property

Gets the number of characters in this instance.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Visual Basic (Declaration)
Public ReadOnly Property Length As Integer
Visual Basic (Usage)
Dim instance As String
Dim value As Integer

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

Property Value

The number of characters in this instance.

The Length property returns the number of Char objects in this instance, not the number of Unicode characters. The reason is that a Unicode character might be represented by more than one Char. Use the System.Globalization.StringInfo class to work with each Unicode character instead of each Char.

The following code example demonstrates the Length property.

Visual Basic
' 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
'
C#
// 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
*/
C++
// 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
*/
J#
// 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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker