Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

ActionScript Programming/PartI/Chapter 3/String

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Data types 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: 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 Method DescriptioncharAt( <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 parametersindexOf ( <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 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 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 # Symbol0  32 [space] 64 @ 96 `1  33 ! 65 A 97 a2  34 " 66 B 98 b3  35 # 67 C 99 c4  36 $ 68 D 100 d5  37 % 69 E 101 e6  38 & 70 F 102 f7  39 ' 71 G 103 g8 * * 40 ( 72 H 104 h9 * * 41 ) 73 I 105 i10 * * 42 * 74 J 106 j11  43 + 75 K 107 k12  44 , 76 L 108 l13 * * 45 - 77 M 109 m14  46 . 78 N 110 n15  47 / 79 O 111 o16  48 0 80 P 112 p17  49 1 81 Q 113 q18  50 2 82 R 114 r19  51 3 83 S 115 s20  52 4 84 T 116 t21  53 5 85 U 117 u22  54 6 86 V 118 v23  55 7 87 W 119 w24  56 8 88 X 120 x25  57 9 89 Y 121 y26  58 : 90 Z 122 z27  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 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 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 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 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 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 This method is discussed later after Array data type. substr 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 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 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 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 length 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); << Previous   Next >>