Compartir a través de


String.Length Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the number of characters in the current string.

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

Syntax

'Declaration
Public ReadOnly Property Length As Integer
public int Length { get; }

Property Value

Type: System.Int32
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.Globalization.StringInfo 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.

Dim charactersBuilder As New StringBuilder("abc")
Dim characters As String

charactersBuilder.Append(ChrW(0))
charactersBuilder.Append("def")

characters = charactersBuilder.ToString()
outputBlock.Text &= characters.Length & vbCrLf
StringBuilder charactersBuilder = new StringBuilder("abc");
string characters;

charactersBuilder.Append('\0');
charactersBuilder.Append("def");

characters = charactersBuilder.ToString();
outputBlock.Text += characters.Length + "\n";

Examples

The following code example demonstrates the Length property.

' Sample for String.Length

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim str As String = "abcdefg"
      outputBlock.Text += String.Format("1) The length of '{0}' is {1}", str, str.Length) & vbCrLf
      outputBlock.Text += String.Format("2) The length of '{0}' is {1}", "xyz", "xyz".Length) & vbCrLf
   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 Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string str = "abcdefg";
      outputBlock.Text += String.Format("1) The length of '{0}' is {1}", str, str.Length) + "\n";
      outputBlock.Text += String.Format("2) The length of '{0}' is {1}", "xyz", "xyz".Length) + "\n";
   }
}
/*
This example produces the following results:
1) The length of 'abcdefg' is 7
2) The length of 'xyz' is 3
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference