How does Actionscript handle the following substrings? For the substrings below, what are the correct outputs?
var main = "1234567890"
var output = substring(main, 5,1) // output =
var output = substring(main, 1,5) // output =
var output = substring(main, 0,5) // output =
var output = substring(main, 5,0) // output =
答案 0 :(得分:2)
使用String.substring
函数给出:
var main:String = "1234567890";
trace(main.substring(5, 1)); // gives : 2345
trace(main.substring(1, 5)); // gives : 2345
trace(main.substring(0, 5)); // gives : 12345
trace(main.substring(5, 0)); // gives : 12345
对于此函数,如果第一个参数(startIndex)大于第二个参数(endIndex),则在函数执行之前自动交换参数。 所以:
string.substring(5, 1) == string.substring(1, 5)
希望可以提供帮助。
答案 1 :(得分:1)
The proper code will be using String.subString()
method, and look like this:
var main:String="1234567890";
var output:String=main.subString(5,1); // output = "2345"
var output:String=main.subString(1,5); // output = "2345"
var output:String=main.subString(0,5); // output = "12345"
var output:String=main.subString(5,0); // output = "12345"
The parameters are startIndex
and endIndex
, they are 0-based, and the character at endIndex
is not included in the returned substring.
EDIT: Indeed the manual states that the parameters are swapped if endIndex
is smaller than startIndex
. Weird, I must say. But if they are equal, the result is an empty string.