slice(String.slice 方法)

public slice(start: Number, end: Number) : String

返回一个字符串,该字符串包括从 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 值赋予它,然后使用 startend 参数的各种值调用 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