Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.

String.Length (Propiedad)


Gets the number of characters in the current String object.

Espacio de nombres:   System
Ensamblado:  mscorlib (en mscorlib.dll)

public int Length { get; }

Valor de propiedad

Type: System.Int32

The number of characters in the current string.

The P:System.String.Length property returns the number of T:System.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 T:System.Char. Use the T:System.Globalization.StringInfo class to work with each Unicode character instead of each T:System.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 P:System.String.Length property returns 7, which indicates that it includes the six alphabetic characters as well as the null character.

using System;
using System.Text;

public class StringClassTest
   public static void Main()
      string characters = "abc\u0000def";
      Console.WriteLine(characters.Length);    // Displays 7

The following example demonstrates the P:System.String.Length property.

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

Universal Windows Platform
Disponible desde 4.5
.NET Framework
Disponible desde 1.1
Portable Class Library
Compatible con: portable .NET platforms
Disponible desde 2.0
Windows Phone Silverlight
Disponible desde 7.0
Windows Phone
Disponible desde 8.1
Volver al principio