4 Replies Latest reply on Sep 13, 2007 4:13 PM by Senor_Roberto

    AS equivalent of CF ListFind()

    swickes
      Using Actionscript, I need to determine if a number is in a list of numbers. Currently the list is a string like "1,2,31"

      How can I determine if the number 3 is in this string without getting a false "match with 31?

      Thanks in advance,
      Steve
        • 1. Re: AS equivalent of CF ListFind()
          Senor_Roberto
          There are 2 approaches, none of them as simple as ListFind (as far as I know).

          First, you'll need to split that list into an array:

          var someString:String = "1,2,31";
          var values:Array = someString.split(",");

          Now that you have an array, you can either try to find it yourself:

          var found = false;

          for each (var n:String in values) {

          if (v == "TestValue") {
          found = true;
          break;
          }

          }

          Or use an ArrayCollection (in Flex anyway...):

          var collection:ArrayCollection = new ArrayCollection(values);


          if (collection.contains("TestValue")) {
          // ...
          }
          • 2. Re: AS equivalent of CF ListFind()
            swickes Level 1
            Gracias Roberto!

            I suspected there must be something like "collection.contains()." That's a good, clear solution.

            Cheers!
            Steve
            • 3. Re: AS equivalent of CF ListFind()
              swickes Level 1
              By the way, in the line:

              if (collection.contains("TestValue"))

              Can "TestValue" be a variable? My code is working with
              if (collection.contains("2"))

              but it does not work with
              if (collection.contains(TestValue))

              Thanks again!
              • 4. Re: AS equivalent of CF ListFind()
                Senor_Roberto Level 1
                contains() actually works on an object, so you have to be a bit careful to make sure that the test comparison and the collection contents are exactly the same type.

                So:

                var testValue:String = "2";

                Then:

                collection.contains(testValue);

                Evaluates to true.