Flash CS3 文档 |
|||
| ActionScript 3.0 编程 > 处理字符串 > 处理字符串中的字符 | |||
字符串中的每个字符在字符串中都有一个索引位置(整数)。第一个字符的索引位置为 0。例如,在以下字符串中,字符 y 的位置为 0,而字符 w 的位置为 5:
"yellow"
您可以使用 charAt() 方法和 charCodeAt() 方法检查字符串各个位置上的字符:
var str:String = "hello world!";
for (var:i = 0; i < str.length; i++)
{
trace(str.charAt(i), "-", str.charCodeAt(i));
}
在运行此代码时,会产生如下输出:
h - 104 e - 101 l - 108 l - 108 o - 111 - 32 w - 119 o - 111 r - 114 l - 108 d - 100 ! - 33
此外,您还可以通过字符代码,使用 fromCharCode() 方法定义字符串,如下例所示:
var myStr:String = String.fromCharCode(104,101,108,108,111,32,119,111,114,108,100,33);
// 将 myStr 设置为“hello world!”
Flash CS3
当前页: http://livedocs.adobe.com/flash/9.0_cn/main/00000080.html