Search code examples
arraysactionscript-3flashactionscript

Actionscript 3 Search for string in array


I have a problem with searching for strings in an array. I want to search for one word and if it exists, I want to trace the position of the string in the array.

I believe it should be something like this:

if (myArray contains "11111111") {
    trace("*position*")
} else {
    trace("cant find it");
}

Solution

  • What kind of syntax are you using there and have you even checked the documentation of the Array class? That should be the first thing you check always, as there is a pretty straightforward method for it:

    var arr:Array = ["1111", "2222", "3333"];
    
    trace(arr.indexOf("3333")); //traces the index: 2