AS3 中 lastIndexOf() 返回的意外值

Unexpected value returned by lastIndexOf() in AS3

lastIndexOf():

返回了一个意外的值
        //cache = "\nWELCOME TO THE LOTTERY GAME\n\n\n\t• Play\nRules\nSettings\n"
        //text = "\t• Play\nRules\nSettings\n"

        var start:int = cache.indexOf(text); //output: start == 31
        var end:int = cache.lastIndexOf(text); //output: end == 31 <- unexpected value

end 应该是 cache.length - 1 = 54。怎么了?

lastIndexOf 不是 return 匹配的结束位置,而是从末尾重新运行匹配搜索的开始索引。如果有多个匹配项,则 indexOf 的不同值将被 returned。例如:

s = "abcab"

这里 indexOf("ab") 会 return 0 而 lastIndexOf("ab") 会 return 3. 但是 lastIndexOf("ab") 不会 return 4. 如果你想要匹配的结束索引,则必须添加匹配字符串的长度。

var end:int = cache.lastIndexOf(text) + text.length;

可能是您预料到了错误的结果 ;)

你应该知道 String.indexOf() and String.lastIndexOf() 之间唯一的区别是搜索的意义,第一个函数是从左边开始(从位置 0 的字符开始),第二个是从右边开始(来自 string.length - 1 位置的字符)并且它们都将 return 您搜索的字符串的第一个字符与调用字符串相比的位置,当然如果它存在的话。

示例:

var parent_string:String = 'hello world';
var parent_string_length:int = parent_string.length;

var child_string:String = 'world';
var child_string_length:int = child_string.length;

// h e l l o _ w o r l d
// 0 1 2 3 4 5 6 7 8 9 10

trace(parent_string.indexOf(child_string));     // gives : 6, it's the position of "w"
trace(parent_string.lastIndexOf(child_string)); // gives : 6, it's the position of "w"

为了更好地理解,我将使用两个 for 循环来执行这些函数的操作:

String.indexOf() : 从左边开始在 "parent_string" 中找到 "child_string" 的第一个索引 :

// h e l l o _ w o r l d
// ->          *
// 0 1 2 3 4 5 6 7 8 9 10

for(var i:int = 0; i < parent_string_length; i++)
{
    var char:String = parent_string.charAt(i);
    var substr:String = parent_string.substr(i, child_string_length);

    trace(i + ') ' + char + ' : ' + substr + ' == ' + child_string + ' : ' + (substr == child_string));

    if(char == child_string.charAt(0)){ // if we find the first char of "child_string" in "parent_string"

        if(substr == child_string){
            // 
            trace('the first index of "' + child_string +'" in "' + parent_string + '", starting from the left, is : ' + i);
            break;
        }
    }
}

这个 for 循环给出:

0) h : hello == world : false
1) e : ello  == world : false
2) l : llo w == world : false
3) l : lo wo == world : false
4) o : o wor == world : false
5)   :  worl == world : false
6) w : world == world : true
the first index of "world" in "hello world", starting from the left, is : 6

String.lastIndexOf() : 从右边开始在 "parent_string" 中找到 "child_string" 的第一个索引 :

// h e l l o _ w o r l d
//             *       <-
// 0 1 2 3 4 5 6 7 8 9 10

for(i = parent_string_length - 1; i > 0; i--)
{
    char = parent_string.charAt(i);
    substr = parent_string.substr(i, child_string_length);

    trace(i + ') ' + char + ' : ' + substr + ' == ' + child_string + ' : ' + (substr == child_string));

    if(char == child_string.charAt(0)){ // if we find the first char of "child_string" in "parent_string"
        if(substr == child_string){
            trace('the first index of "' + child_string +'" in "' + parent_string + '", starting from the right, is : ' + i);
            break;
        }
    }
}

这个for循环给出:

10) d : d == world : false
9) l : ld == world : false
8) r : rld == world : false
7) o : orld == world : false
6) w : world == world : true
the first index of "world" in "hello world", starting from the right, is : 6

希望事情更清楚,以及所有可以提供帮助的东西。