ActionScript Programming/PartI/Chapter 3/String

Data types

edit

String

edit

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:

 var <name> = new String( [expression] );

Parameter <name> 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.

Methods

edit
Method Description
charAt( <index> ) Returns the character in the position specified by the parameter <index>.
charCodeAt( <index> ) Returns the code of the specified character.
concat( <val1>, …, <valN>) Combines the value of the String object with the parameters and returns the newly formed string.
fromCharCode( <c1>, … <cN> ) returns a string made up of the characters represented by the character codes (ASCII values) in the parameters
indexOf ( <strsearch>, [startindex] ) searches the string and returns the position of the first occurrence of <strsearch> found at or after [startindex] within the calling string.
lastIndexOf( <strsearch>, [startindex] ) searches the string from right to left and returns the index of the last occurrence of <strsearch> found before [startindex] within the calling string.
slice( <startindex>, [endindex] ) returns a string that includes the <startindex> character and all characters up to (but not including) the [endindex] character.
split( <separator>, [limit] ) splits a String object into substrings by breaking it wherever the specified <separator> parameter occurs, and returns the substrings in an array.
substr( <startindex>, [length] ) returns the characters in a string from the index specified in the <startindex> parameter through the number of characters specified in the [length] parameter.
substring( <startindex>, [endindex] ) returns a string consisting of the characters between the points specified by the <startindex> and [endindex] parameters.
toLowerCase() returns a copy of the String object, with all of the uppercase characters converted to lowercase.
toUpperCase() returns a copy of the String object, with all of the lowercase characters converted to uppercase.
charAt
edit

String.charAt( <index> );

This method returns the character in the position specified by the parameter <index>. 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 <index> parameter, for reading the third character you must put 2 as <index>. Now lets write a program which will get the second character of string “hello”.

 1.   var tmpString = new String("hello");
 2.   trace(tmpString.charAt(1));
charCodeAt
edit

String.charCodeAt( <index> );

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:

# Symbol # Symbol # Symbol # Symbol
0 ? 32 [space] 64 @ 96 `
1 ? 33 ! 65 A 97 a
2 ? 34 " 66 B 98 b
3 ? 35 # 67 C 99 c
4 ? 36 $ 68 D 100 d
5 ? 37 % 69 E 101 e
6 ? 38 & 70 F 102 f
7 ? 39 ' 71 G 103 g
8 * * 40 ( 72 H 104 h
9 * * 41 ) 73 I 105 i
10 * * 42 * 74 J 106 j
11 ? 43 + 75 K 107 k
12 ? 44 , 76 L 108 l
13 * * 45 - 77 M 109 m
14 ? 46 . 78 N 110 n
15 ? 47 / 79 O 111 o
16 ? 48 0 80 P 112 p
17 ? 49 1 81 Q 113 q
18 ? 50 2 82 R 114 r
19 ? 51 3 83 S 115 s
20 ? 52 4 84 T 116 t
21 ? 53 5 85 U 117 u
22 ? 54 6 86 V 118 v
23 ? 55 7 87 W 119 w
24 ? 56 8 88 X 120 x
25 ? 57 9 89 Y 121 y
26 ? 58 : 90 Z 122 z
27 ? 59 ; 91 [ 123 {
28 ? 60 < 92 \ 124
29 ? 61 = 93 ] 125 }
30 ? 62 > 94 ^ 126 ~
31 ? 63 ? 95 _ 127 ?

?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”.

 1.   var tmpString = new String("hello");
 2.   trace(tmpString.charCodeAt(0));
 3.   trace(tmpString.charCodeAt(1));
 4.   trace(tmpString.charCodeAt(2));
 5.   trace(tmpString.charCodeAt(3));
 6.   trace(tmpString.charCodeAt(4));
concat
edit

String.concat( <val1>, …, <valN> );

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:

 1.   var tmpString = new String("Hello!!!");
 2.   tmpString = tmpString.concat(" How", " are", " you?");
 3.   trace(tmpString);
fromCharCode
edit

String.fromCharCode( <c1>, …, <cN> );

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:

 1.   var tmpString = new String();
 2.   tmpString = String.fromCharCode(104,101,108,108,111);
 3.   trace(tmpString);
indexOf
edit

String.indexOf( <strsearch>, [startindex] );

This method searches the string you specified by the parameter <strsearch> 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”:

 1.   var tmpString = new String("hello!!!");
 2.   trace(tmpString.indexOf("ll"));

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.

 1.   var tmpString = new String("hello!!!");
 2.   trace(tmpString.indexOf("ll",3));
lastIndexOf
edit

String.lastIndexOf( <strsearch>, [startindex] );

This method searches the string you specified by the parameter <strsearch> 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”:

 1.   var tmpString = new String("ZoZZooZZoZoZZ");
 2.   trace(tmpString.lastIndexOf("ZZ"));

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:

 1.   var tmpString = new String("ZoZZooZZoZoZZ");
 2.   trace(tmpString.lastIndexOf("ZZ",9));
slice
edit

String.slice( <startindex>, [endindex] );

This method returns a string that includes the <startindex> 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 <startindex> 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?”:

 1.   var tmpString = new String("How are you?");
 2.   trace(tmpString.slice(0,3));

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

 1.   var tmpString = new String("How are you?");
 2.   trace(tmpString.slice(4));
split
edit

This method is discussed later after Array data type.

substr
edit

String.substr( <startindex>, [length] );

This method returns the characters in a string from the index specified in the <startindex> 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:

 1.   var tmpString = new String("How are you?");
 2.   trace(tmpString.substr(4,3));
substring
edit

String.substring( <startindex>, [endindex] );

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

 1.   var tmpString = new String("How are you?");
 2.   trace(tmpString.substring(0,2));
toLowerCase
edit

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”:

 1.   var tmpString = new String("Hello");
 2.   trace(tmpString.toLowerCase());
toUpperCase
edit

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”:

 1.   var tmpString = new String("Hello");
 2.   Trace(tmpString.toUpperCase());

Properties

edit
length
edit

String.length

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

 1.   var tmpString = new String("hello");
 2.   trace(tmpString.length);