Dlean Jeans
Dlean Jeans

Reputation: 996

Unexpected value returned by lastIndexOf() in AS3

I'm having an unexpected value returned by 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 is supposed to be cache.length - 1 = 54. What's wrong with it?

Upvotes: 0

Views: 278

Answers (2)

akmozo
akmozo

Reputation: 9839

May be you have expected the wrong result ;)

You should know that the only difference between String.indexOf() and String.lastIndexOf() is the sens of searching, the first function is starting from the left ( from the char at position 0 ) and the second one is starting from the right ( from the char at position string.length - 1 ) and both of them will return the position of the first char of your searched string compared to the calling string, if it exists of course.

Example :

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"

To more understand, I will use two for loops to do what these functions do :

String.indexOf() : find the first index of "child_string" in "parent_string" starting from the left :

// 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;
        }
    }
}

This for loop gives :

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() : find the first index of "child_string" in "parent_string" starting from the right :

// 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;
        }
    }
}

This for loop gives :

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

Hope that things are more clear, and all that can help.

Upvotes: 0

taskinoor
taskinoor

Reputation: 46027

lastIndexOf does not return the end position of a match, rather it reruns the beginning index of a match searching from the end. Different value from indexOf is returned if there are multiple matches. For example:

s = "abcab"

Here indexOf("ab") will return 0 and lastIndexOf("ab") will return 3. But lastIndexOf("ab") won't return 4. If you want the end index of a match then you have to add the length of matched string.

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

Upvotes: 1

Related Questions