AS: Strings 2005-08-30 13:23:30
This will cover the String object data type, it's explained in
AS: Variables by Rantzien but the String object has a lot of methods primitive variables don't.
What are strings?
Strings are used to store a lot of characters, specifically, words , text and other word-related things. for example "Hello" is a string, "Welcome Home" is a string, "I love pie and I love" + newline + "LOLZ" is a string
I will devide this tutorial to 3 parts,
1)What ARE strings?
What strings are
Strings are really constant character arrays, what does it mean? it means that it's an ordered bunch of letters, a character is a single letter and an array is a bunch of variables one after another... the fact that it's constant means that whenever you modify an array you actually distroy it and recreate it modified,
For what you generally need to know Strings are words.
Another importent thing you need to know about Strings in flash is that a string in flash is an Object, it has properties like it's actual content and other importent properties like "lentgh", and methods like "slice" "join" and others
To declare a string you simple use the var command like you do with a var
var string_name; //partially
var string_name:String; //fully
you can also add a value to it like so:
var string_name:String= "Hello";
Strings are used for many things, names, message boxes, and generally any form of text you store. Since strings are primitive objects in flash tehy have very easy to use functions.
The 2 things you'll probebly use the most with strings, are the "+" operator, and the charAt method.
The + operator
With strings, the addition (+) operator is used to append one string for another, for example "hello" + " world" would be equal to writing "hello world", you can do it with string variables too:
var sa:String=" world";
trace(s+sa); //traces "hello world"
trace(sa+s); //traces " worldhello"
trace("hello" + s);//traces "hellohello"
youy can also do
making s equal now to "hello world";
or in short:
producing the same effect.
sometimes you'll want to access a specific character in a string, this is done with the String.charAt(position); command, you can just do name.charAt(0) for the first char or for example s.charAt(2); for the 3rd char, in this case 'l'.
The others summerized from liveDocs
An importent property you'll use is String.length it contains the length of a string, for example s.length would return 5.
String.charCodeAt() Returns the value of the character at the specified index as a 16-bit integer between 0 and 65535.
String.concat() Combines the text of two strings and returns a new string. for example s.concat(sa) would return hello world, just like the + operator.
String.fromCharCode(index:Number) Returns a string comprising the characters specified in the parameters.
String.indexOf() Returns the position of the first occurrence of a specified substring. for example s.indexOf("l"); would return 2 since it's the first place it appears, take note that index is 1 character before the location.
String.lastIndexOf() Returns the position of the last occurrence of a specified substring. s(lastIndexOf("l"); would return 3, because it's the last place "l" appears, take note that index is 1 character before the location.
String.slice() Extracts a section of a string and returns a new string. for example s.slice(3) would return lo, s.slice(1,3) would return elo
String.split() Splits a String object into an array of strings by separating the string into substrings. this one is by a specific character, look into it's livedocs, it's importent and worthy to check out.
String.substr() Returns a specified number of characters in a string, beginning at a specified location.
String.substring() Returns the characters between two indexes in a string.
String.toLowerCase() Converts the string to lowercase and returns the result; does not change the contents of the original object.
String.toUpperCase() Converts the string to uppercase and returns the result; does not change the contents of the original object.