Flash CS3 文档 |
|||
| ActionScript 2.0 语言参考 > ActionScript 类 > String > slice(String.slice 方法) | |||
返回一个字符串,该字符串包括从 start 字符一直到 end 字符(但不包括该字符)之间的所有字符。不修改原始 String 对象。如果未指定 end 参数,则子字符串的结尾就是该字符串的结尾。如果按 start 索引到的字符与按 end 索引到的字符相同或位于后者的右侧,则该方法返回一个空字符串。
可用性:ActionScript 1.0、Flash Player 5
start:Number ― 片段起始点的从零开始的索引。如果 start 是一个负数,则起始点从字符串的结尾开始确定,其中 -1 表示最后一个字符。
end:Number ― 一个比片段终点索引大 1 的整数。由 end 参数索引的字符未包括在已提取的字符串中。如果省略此参数,则使用 String.length。如果 end 是一个负数,则终点根据从字符串的结尾向后数确定,其中 -1 表示最后一个字符。
String ― 指定字符串的子字符串。
下面的示例创建一个变量 my_str,,将 String 值赋予它,然后使用 start 和 end 参数的各种值调用 slice() 方法。对 slice() 的每个调用都包装在 trace() 语句中,该语句将输出显示在“输出”面板中。
// Index values for the string literal
// positive index: 0 1 2 3 4
// string: L o r e m
// negative index: -5 -4 -3 -2 -1
var my_str:String = "Lorem";
// slice the first character
trace("slice(0,1): "+my_str.slice(0, 1)); // output: slice(0,1): L
trace("slice(-5,1): "+my_str.slice(-5, 1)); // output: slice(-5,1): L
// slice the middle three characters
trace("slice(1,4): "+my_str.slice(1, 4)); // slice(1,4): ore
trace("slice(1,-1): "+my_str.slice(1, -1)); // slice(1,-1): ore
// slices that return empty strings because start is not to the left of end
trace("slice(1,1): "+my_str.slice(1, 1)); // slice(1,1):
trace("slice(3,2): "+my_str.slice(3, 2)); // slice(3,2):
trace("slice(-2,2): "+my_str.slice(-2, 2)); // slice(-2,2):
// slices that omit the end parameter use String.length, which equals 5
trace("slice(0): "+my_str.slice(0)); // slice(0): Lorem
trace("slice(3): "+my_str.slice(3)); // slice(3): em
有关其它示例,请参阅位于 www.adobe.com/go/learn_fl_samples_cn 的 Flash 示例页。下载并解压缩示例 zip 文件,然后进入 ActionScript2.0\Strings 文件夹以访问 Strings.fla 文件。
substr(String.substr 方法), substring(String.substring 方法)
Flash CS3
当前页: http://livedocs.adobe.com/flash/9.0_cn/main/00002165.html