Export (0) Print
Expand All

String.Length Property

Gets the number of characters in this instance.

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

public int Length { get; }
/** @property */
public int get_Length ()

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.

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

Community Additions

ADD
Show:
© 2014 Microsoft