.NET Framework Class Library
StringLength Property

Gets the number of characters in the current String object.

Namespace:   System
Assemblies:   System.Runtime (in System.Runtime.dll)
  mscorlib (in mscorlib.dll)
Syntax
Public ReadOnly Property Length As [%$TOPIC/463se318_en-us_VS_110_2_0_0_0_0%]
public [%$TOPIC/463se318_en-us_VS_110_2_0_1_0_0%] Length { get; }
public:
property [%$TOPIC/463se318_en-us_VS_110_2_0_2_0_0%] Length {
	[%$TOPIC/463se318_en-us_VS_110_2_0_2_0_1%] get ();
}
member Length : [%$TOPIC/463se318_en-us_VS_110_2_0_3_0_0%] with get

Property Value

Type: SystemInt32
The number of characters in the current string.
Remarks

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.GlobalizationStringInfo class to work with each Unicode character instead of each Char.

In some languages, such as C and C++, a null character indicates the end of a string. In the .NET Framework, a null character can be embedded in a string. When a string includes one or more null characters, they are included in the length of the total string. For example, in the following string, the substrings "abc" and "def" are separated by a null character. The Length property returns 7, which indicates that it includes the six alphabetic characters as well as the null character.

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
using System;
using System.Text;

public class StringClassTest
{
   public static void Main()
   {
      string characters = "abc\u0000def";
      Console.WriteLine(characters.Length);    // Displays 7
   }
}
using namespace System;
using namespace System::Text;

void main()
{
   String^ characters = "abc" + L'0' + "def";
   Console::WriteLine(characters->Length);        // Displays 7
}
Examples

The following example demonstrates the Length property.

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 'Main
End Class 'Sample
' 
'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
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);

       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
// 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
*/
Version Information

.NET Framework

Supported in: 4.6, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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