ActionScript 程式設計/第一部分/第三章/字串
在上一節中,您瞭解了字串和數字資料型別之間的區別。現在我們將詳細分析字串資料型別。以下是宣告字串變數的結構
var <name> = new String( [expression] );
引數 <name> 是您要宣告的變數的名稱,引數 [expression] 是您想要在建立變數時賦予它的值。
| 方法 | 描述 |
|---|---|
| charAt( <index> ) | 返回引數 <index> 指定位置的字元。 |
| charCodeAt( <index> ) | 返回指定字元的程式碼。 |
| concat( <val1>, …, <valN>) | 將字串物件的 value 與引數組合,並返回新形成的字串。 |
| fromCharCode( <c1>, … <cN> ) | 返回由引數中的字元程式碼(ASCII 值)表示的字元組成的字串 |
| indexOf ( <strsearch>, [startindex] ) | 搜尋字串,並在呼叫字串中返回在 [startindex] 或之後找到的 <strsearch> 的第一次出現的索引位置。 |
| lastIndexOf( <strsearch>, [startindex] ) | 從右到左搜尋字串,並在呼叫字串中返回在 [startindex] 之前找到的 <strsearch> 的最後一次出現的索引位置。 |
| slice( <startindex>, [endindex] ) | 返回一個字串,其中包括 <startindex> 字元和一直到(但不包括)[endindex] 字元的所有字元。 |
| split( <separator>, [limit] ) | 透過在指定 <separator> 引數出現的地方將字串物件分割成子字串來分割字串物件,並將子字串返回到陣列中。 |
| substr( <startindex>, [length] ) | 返回字串中從 <startindex> 引數中指定的索引到 [length] 引數中指定的字元數量的字元。 |
| substring( <startindex>, [endindex] ) | 返回一個字串,該字串包含 <startindex> 和 [endindex] 引數指定的點之間的字元。 |
| toLowerCase() | 返回字串物件的副本,其中所有大寫字元都轉換為小寫。 |
| toUpperCase() | 返回字串物件的副本,其中所有小寫字元都轉換為大寫。 |
String.charAt( <index> );
此方法返回引數 <index> 指定位置的字元。字串的第一個字元用 0 表示,因此如果要讀取指定字串的第一個字元,則必須將 0 作為 <index> 引數,要讀取第三個字元,則必須將 2 作為 <index>。現在,讓我們編寫一個程式來獲取字串“hello”的第二個字元。
1. var tmpString = new String("hello");
2. trace(tmpString.charAt(1));
String.charCodeAt( <index> );
為了理解此方法的作用,您首先必須瞭解一些有關鍵盤和“ASCII 值”的知識。螢幕上列印的每個“符號”都有一個等效程式碼。例如,大寫字母“A”的程式碼是 65。在“ASCII”美國標準中,有 127 個字元程式碼。世界上所有的計算機都支援 ASCII 標準。在這個字元集中(字元程式碼從 0 到 127),只有符號的標準程式碼。例如,符號“©”在 Microsoft® Windows® 中的程式碼是 165。在其他作業系統中,它可能是另一個程式碼。以下是符號與其等效程式碼的列表
| # | 符號 | # | 符號 | # | 符號 | # | 符號 |
|---|---|---|---|---|---|---|---|
| 0 | ? | 32 | [空格] | 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 ? |
?這些字元不受 Microsoft Windows 支援。
* *值 8、9、10 和 13 分別轉換為退格鍵、製表鍵、換行符和回車符。它們沒有圖形表示,但根據應用程式的不同,可能會影響文字的視覺顯示。
您將在以後瞭解此方法的用法。現在您必須知道這一點。現在,讓我們編寫一個程式來列印字串“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));
String.concat( <val1>, …, <valN> );
此方法將字串物件的 value 與引數組合,並返回新形成的字串。但請注意,它不會更改字串,它只是返回它。此示例顯示了此方法的用法
1. var tmpString = new String("Hello!!!");
2. tmpString = tmpString.concat(" How", " are", " you?");
3. trace(tmpString);
String.fromCharCode( <c1>, …, <cN> );
此方法返回由引數中的字元程式碼(ASCII 值)表示的字元組成的字串。在此方法中,“String” 絕不能替換為字串變數的名稱,例如:“mystring.fromCharCode”,它必須保留為“String”。在這種情況下,“String” 代表字串物件。以下示例建立了一個“hello”字串
1. var tmpString = new String();
2. tmpString = String.fromCharCode(104,101,108,108,111);
3. trace(tmpString);
String.indexOf( <strsearch>, [startindex] );
此方法搜尋您透過引數 <strsearch> 指定的字串,並返回第一次出現的索引位置。如果未找到字串,則返回 -1。例如,此程式碼在“tmpString”變數中搜索“ll”字串,該變數的值為“hello”
1. var tmpString = new String("hello!!!");
2. trace(tmpString.indexOf("ll"));
“indexOf” 方法將返回 2,這意味著您要查詢的字串的開頭位於字元 3 處(不要忘記字串的第一個字元是 0)。引數 [startindex] 告訴 Flash 從指定字元開始搜尋字串。在以下示例中,“indexOf” 方法返回的值為 -1,這意味著未找到該字串。
1. var tmpString = new String("hello!!!");
2. trace(tmpString.indexOf("ll",3));
String.lastIndexOf( <strsearch>, [startindex] );
此方法搜尋引數 <strsearch> 指定的字串,並返回其最後一次出現的的位置。如果未找到該字串,則返回 -1。例如,此程式碼在“tmpString” 變數中搜索“ZZ” 字串,該變數的值為“ZoZZooZZoZoZZ”。
1. var tmpString = new String("ZoZZooZZoZoZZ");
2. trace(tmpString.lastIndexOf("ZZ"));
“indexOf” 方法將返回 11,這意味著您要查詢的字串的開頭(從字串的末尾開始)位於字元 11 處(不要忘記字串的第一個字元是 0)。引數 [startindex] 告訴 Flash 從指定字元開始搜尋字串。這意味著指定字元之後的那部分字串不參與搜尋過程。在以下示例中,“indexOf” 方法返回的值為 6
1. var tmpString = new String("ZoZZooZZoZoZZ");
2. trace(tmpString.lastIndexOf("ZZ",9));
String.slice( <startindex>, [endindex] );
此方法返回包含 <startindex> 字元和直到(但不包括)[endindex] 字元的所有字元的字串。如果您沒有指定 [endindex] 引數,則返回值將包含 <startindex> 字元和直到字串末尾的所有字元的字串。在此示例中,我們從字串“How are you?” 中切出單詞“How”。
1. var tmpString = new String("How are you?");
2. trace(tmpString.slice(0,3));
現在,讓我們切出從 4 到字串末尾的所有字元
1. var tmpString = new String("How are you?");
2. trace(tmpString.slice(4));
此方法將在討論完陣列資料型別後進行介紹。
String.substr( <startindex>, [length] );
此方法返回字串中從 <startindex> 引數中指定的索引開始的字元,直到 [length] 引數中指定的字元數量。它與“slice” 方法幾乎相同,但在此函式中,您指定要剪下的字串的長度,而不是最後一個字元的索引。此示例說明了此方法的使用方式
1. var tmpString = new String("How are you?");
2. trace(tmpString.substr(4,3));
String.substring( <startindex>, [endindex] );
此方法返回一個字串,該字串包含 <startindex> 和 [endindex] 引數指定的點之間的字元。如果未指定 [endindex] 引數,則返回的字串包含從點 <startindex> 到字串末尾的字元。此示例說明了此方法的使用方式
1. var tmpString = new String("How are you?");
2. trace(tmpString.substring(0,2));
String.toLowerCase( );
此方法返回指定字串的副本,其中所有大寫字母都轉換為小寫字母。例如,字串“Hello” 將轉換為“hello”。
1. var tmpString = new String("Hello");
2. trace(tmpString.toLowerCase());
String.toUpperCase( );
此方法返回指定字串的副本,其中所有小寫字母都轉換為大寫字母。例如,字串“Hello” 將轉換為“HELLO”。
1. var tmpString = new String("Hello");
2. Trace(tmpString.toUpperCase());
String.length
此方法返回字串的長度。例如,字串“hello” 的長度將為 5
1. var tmpString = new String("hello");
2. trace(tmpString.length);