ActionScript Programming/PartI/Chapter 3/String

String
In the previous section you understood the difference between String and Number data types. Now we will analyze String data type in details. The following is the structure of declaring String variable:

Parameter is the name of the variable you want to declare, and the parameter [expression] is the value you want the variable to be assigned when it is created.

charAt
String.charAt;

This method returns the character in the position specified by the parameter. The first character of the string is indicated by 0, so if you want to read the first character of the specified string you must put 0 as parameter, for reading the third character you must put 2 as. Now lets write a program which will get the second character of string “hello”.

charCodeAt
String.charCodeAt;

For understanding what this method does you must first know something about the keyboard and “ASCII value”. Each “symbol”, which is printed on the screen has an equivalent code. For example the code of the capital letter “A” is 65. In the American Standard(“ASCII”) there are 127 character codes. All computers in the world support ASCII standard. In this character set (character codes from 0 to 127) are only the standard codes of the symbols. For example the code of the symbol “©” in Microsoft® Windows® is 165. In other operating systems it can be another code. Here is the list of symbols with their equivalent codes:

These characters aren't supported by Microsoft Windows.


 * *Values 8, 9, 10, and 13 convert to backspace, tab, linefeed, and carriage return characters, respectively. They have no graphical representation but, depending on the application, can affect the visual display of text.

You will understand the use of this method later. For now you must know this. Now lets write a program which will print all codes of the characters of string “hello”.

concat
String.concat(, …,  );

This method combines the value of the String object with the parameters and returns the newly formed string. But note, it doesn’t change the string, it just returns it. This example shows the use of this method:

fromCharCode
String.fromCharCode( , …,  );

This method returns a string made up of the characters represented by the character codes (ASCII values) in the parameters. In this method “String” must not be replaced with a name of String variable for example: “mystring.fromCharCode”, it must be left as “String”. In this case “String” is represented by as a String object. The following example creates a “hello” string:

indexOf
String.indexOf(, [startindex] );

This method searches the string you specified by the parameter and returns the position of the first occurrence of it. If the string is not founded it returns -1. For example this code searches for “ll” string in “tmpString” variable, which’s value is “hello”:

The method “indexOf” will return 2, which means that the beginning of the string you was looking for is at character 3(don’t forget that the first character of the string is 0). The parameter [startindex] tells Flash to begin searching the string from the specified character. In the following example the value returned by “indexOf” method is -1, which means, that the string wasn’t found.

lastIndexOf
String.lastIndexOf(, [startindex] );

This method searches the string you specified by the parameter and returns the position of the last occurrence of it. If the string is not founded it returns -1. For example this code searches for “ZZ” string in “tmpString” variable, which’s value is “ZoZZooZZoZoZZ”:

The method “indexOf” will return 11, which means that the beginning of the string you was looking for, from the end of the string, is at character 11(don’t forget that the first character of the string is 0). The parameter [startindex] tells Flash to begin searching the string from the specified character. This means that the part of the string which is followed after the specified character is not involved in the search process. In the following example the value returned by “indexOf” method is 6:

slice
String.slice(, [endindex] );

This method returns a string that includes the character and all characters up to (but not including) the [endindex] character. If you do not specify [endindex] parameter then the return value will be a string that includes the character and all characters up to the end of the String. In this example we slice the word “How” from the String “How are you?”:

Now lets slice all characters from 4 till the end of the String:

split
This method is discussed later after Array data type.

substr
String.substr(, [length] );

This method returns the characters in a string from the index specified in the parameter through the number of characters specified in the [length] parameter. It is nearly the same as method “slice” but in this function you specify the length of the string to be cut, not the index of the last character. This example illustrates the use of this method:

substring
String.substring(, [endindex] );

This method returns a string consisting of the characters between the points specified by the and [endindex] parameters. If the parameter [endindex] is not specified then the string which is returned consists of the characters from the point till the end of the string. This example illustrates the use of this method:

toLowerCase
String.toLowerCase;

This method returns a copy of the specified String, with all of the uppercase characters converted to lowercase. For example a string “Hello” will be converted to “hello”:

toUpperCase
String.toUpperCase;

This method returns a copy of the specified String, with all of the lowercase characters converted to uppercase. For example a string “Hello” will be converted to “HELLO”:

length
String.length

This method returns the length of the string. For example the length of string “hello” will be 5:

<< Previous &emsp; Next >>