Name

String.length Property — the number of characters in a string

Availability

Flash 5

Synopsis

string.length

Access

Read-only

Description

The length property returns the number of characters in string. Note that a null character (ASCII 0) does not signal the end of a string, as it would in some languages, but neither is it counted in the string’s length. For example:

// Create the string "A" + null + "B"
var myString = String.fromCharCode(65,0,66);
trace(myString.length);  // Displays: 2 (The null character is ignored)

Example

var myString = "hello";
trace (myString.length);  // Displays: 5
trace ("hello".length);   // Displays: 5

// Here we convert the number 1000 to a
// string in order to test its length
var age = 1000;
// Display an error message if the number has the wrong number of digits.
if (String(age).length != 2) {
  trace ("Please enter a two-digit number");
}

See Also

Array.length( ); Section 4.6.5.1 in Chapter 4

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.