2 Replies Latest reply on Jan 19, 2007 1:44 PM by Newsgroup_User

    array access notation with strings?

    DZ-015 Level 1
      I may be confused, but I thought you could use array access notation with strings. Can someone tell me why this code doesn't work (or suggest new code):

      the code is meant to rewrite the block of text in tText at a rate of one character per frame... (when the function is called once per frame) but it doesn't work.
        • 1. Re: array access notation with strings?
          Level 7
          DZ-015,

          > I may be confused, but I thought you could use
          > array access notation with strings.

          It's made for objects, actually. The array access operator, [], allows
          you to access elements from an Array instance and also lets you access
          properties from any sort of object. The "trick" it provides, in this latter
          case, is that you can refer to these properties -- within the brackets --
          with strings, so it's often used to reference objects dynamically.

          > var sWorkingText:String = tText.text;

          Here you've declared a String variable and set it to the TextField.text
          property of some textfield. So far, so good.

          > var sNewText:String = oPhotoText[sPageFocus];

          Here, you've declared a String variable, but it seems that you're
          setting it to a property of the oPhotoText object. That's now how this
          works.

          The array access operator allows you to *use* strings to get at an
          object's properties:

          // functionally equivalent
          someObject.property1
          someObject["property" + 1];
          var x:Number = 1;
          someObject["property" + x];

          Does that help?


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. array access notation with strings?
            Juankpro Level 2
            Nope you cannot use array acces notation with strings. If you want to read a letter use the chatAt() method this way:

            sNewText.charAt(nIndex);

            this example reads the a character in the nIndex index;