Flash CS3 Documentation |
|||
| ActionScript 2.0 Language Reference > ActionScript classes > String > length (String.length property) | |||
public length : Number
An integer specifying the number of characters in the specified String object.
Because all string indexes are zero-based, the index of the last character for any string x is x.length - 1.
Availability: ActionScript 1.0; Flash Player 5
The following example creates a new String object and uses String.length to count the number of characters:
var my_str:String = "Hello world!"; trace(my_str.length); // output: 12
The following example loops from 0 to my_str.length. The code checks the characters within a string, and if the string contains the @ character, true displays in the Output panel. If it does not contain the @ character, then false displays in the Output panel.
function checkAtSymbol(my_str:String):Boolean {
for (var i = 0; i<my_str.length; i++) {
if (my_str.charAt(i) == "@") {
return true;
}
}
return false;
}
trace(checkAtSymbol("dog@house.net")); // output: true
trace(checkAtSymbol("Chris")); // output: false
For another example, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download and decompress the Samples zip file and go to the ActionScript2.0\Strings folder to access the Strings.fla file.
Flash CS3