5 Replies Latest reply on Sep 8, 2006 7:40 AM by Newsgroup_User

    removing a character from a string

    Goo101 Level 1
      Hi All,

      Back yet again with another thing Flash doesn't do well!

      I have a file path which I've put together by stringing a number of constants and variables together, as per the following line of code . . .

      var imagePath:String = String("C:\\local audio\\" + VoucherNo + "\\" + Artist + "\\" + Title + "\\folder.jpg");

      The above code generates the following path name . . .
      C:\local audio\20060907161501\The Cranberries\No Need to Argue: The Complete Sessions 1994-1995\folder.jpg

      This works fine except for the fact that the 'Title' variable contains a colon (:) and you can't have any special characters such as a colon in a path name. So, in order to pull in the .jpg image from this path, I want to remove the colon from the path thus giving me a valid path in a new string.

      I have been able to pinpoint the index of the colon at position 62 using the following code . . .

      if(imagePath.lastIndexOf(":") != -1) {
      var i:Number = Number(imagePath.lastIndexOf(":"));
      trace(i);
      }

      But I do not know how to simply remove the colon from the string at this position . . . I have tried the following line of code but with no sucess as the "removeCharAt(i)" code does not exist in AS2 . . .

      var newImagePath:String = imagePath.removeCharAt(i);

      Is there any other way that I can remove this special character at this position without having to splice the string to create 2 new variables and then string them back together, thus overcoming the colon problem!?

      Any help anyone could give would be much appreciated! Thanks.
        • 1. Re: removing a character from a string
          Level 7
          Goo101,

          > Back yet again with another thing Flash doesn't do well!

          What on earth!? Your subject line reads "removing a character from a
          string," and you're saying Flash doesn't do this well? Horsefeathers! :-p
          Flash provides you with the tools ... in this case, it's you who hasn't
          figured it out yet.

          > So, in order to pull in the .jpg image from this path, I want
          > to remove the colon from the path thus giving me a valid
          > path in a new string.

          I'm curious how that colon got there in the first place, because you're
          right, colons aren't allowed in Windows file paths.

          > I have been able to pinpoint the index of the colon at position
          > 62 using the
          > following code . . .
          >
          > if(imagePath.lastIndexOf(":") != -1) {
          > var i:Number = Number(imagePath.lastIndexOf(":"));
          > trace(i);
          > }

          Okay. So far, so good.

          > But I do not know how to simply remove the colon from
          > the string at this position . . . I have tried the following line
          > of code but with no sucess as the "removeCharAt(i)" code
          > does not exist in AS2 . . .
          >
          > var newImagePath:String = imagePath.removeCharAt(i);

          I'm always baffled (and sometimes amused) why people just "try" a
          totally made up bit of ActionScript. All you have to do to find out what
          functionality is possible with strings, is to look up the String class.
          This is a useful tip to know when dealing with just about any aspect of
          ActionScript. ActionScript is object-oriented, so you're generally dealing
          with objects. If you want to know about movie clips, look up the "MovieClip
          class" entry. If you're dealing with text fields, look up the "TextField
          class". Here with strings, look up the String class, and you'll find at
          least one of three categories that defines your object: properties
          (characteristics), methods (things the object can do), and events (things
          the object can react to).

          > Is there any other way that I can remove this special character
          > at this position without having to splice the string to create 2
          > new variables and then string them back together, thus
          > overcoming the colon problem!?

          There happens to be a splice() method (method of the Array class), so
          I'm not sure if that's what you meant. The String class features a split()
          method that lets you convert a string to an array, based on a character
          (such as colon), then you can convert the array back into a string.

          In this case, I might just use the String.substr() method twice. No
          need for new variables.

          var str:String = "this-and-that";
          str = str.substr(0, str.lastIndexOf("-")) + str.substr(str.lastIndexOf("-")
          + 1);
          trace(str);

          Look carefully ... to see what I've done. It's one of many ways to
          handle this scenario.

          Keep your chin up. ActionScript gives you a fantastic toolset. ;) Just
          think through what you've got and put it to use.


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


          • 2. Re: removing a character from a string
            Goo101 Level 1
            Thank you David!

            "Horsefeathers!"? I think "horseshit!" was the word you were looking for ;-) . . . I didn't mean to slag Flash off, I simply thought I seen a removeCharAt() method, it was probably the removeItemAt() method I was thinking of.

            The code you gave me works fine btw, so thank you very much for that.

            The reason the path had a colon in it, is that I have pulled in the 'Title' variable to help create the path and it is this variable which contains the colon I need to remove in order to get a valid path.

            Thanks again . . .
            • 3. Re: removing a character from a string
              Goo101 Level 1
              Ok, what's wrong with the following code . . .

              var imagePath:String = ("C:\\local audio\\" + VoucherNo + "\\" + Artist + "\\" + Title + "\\folder.jpg");
              if(imagePath.lastIndexOf(":") != -1) {
              var i:Number = Number(imagePath.lastIndexOf(":"));
              var newImagePath = imagePath.substr(0, imagePath.lastIndexOf(":")) + imagePath.substr(imagePath.lastIndexOf(":") + 1);
              trace(newImagePath);
              trace(i);
              }
              var mImageLoader:MovieClipLoader = new MovieClipLoader();
              mImageLoader.loadClip(newImagePath, imageLoader);

              The newImagePath variable gives the following path . . .

              C:\local audio\20060907161501\The Cranberries\No Need to Argue The Complete Sessions 1994-1995\folder.jpg

              But yet I am getting the following error in the output panel . . .

              Error opening URL "file:///C:\local audio\20060907161501\The Cranberries\No Need to Argue The Complete Sessions 1994-1995\folder.jpg"

              Am I missing something here!?
              • 4. Re: removing a character from a string
                Goo101 Level 1
                Sorry, please ignore the last posting, my problem was that the image is named "folder.jpg" so the path should end in "folder.jpg.jpg" in order to pick up the image! I couldn't understand what was wrong there but now I've finally figured it out!
                • 5. Re: removing a character from a string
                  Level 7
                  Goo101,

                  > "Horsefeathers!"? I think "horseshit!" was the word you
                  > were looking for ;-) .

                  Ha! Nah ... horseshit is a bit strong. Horsefeathers usually gets a
                  chuckle. Life's too short to frown. ;)

                  > . . I didn't mean to slag Flash off, I simply thought I seen
                  > a removeCharAt() method, it was probably the removeItemAt()
                  > method I was thinking of.

                  Oh, Flash can take it. ;) My main concern is to help people "get"
                  ActionScript, and I often see folks frustrated with what they perceive as a
                  weak toolset when, in fact, they simply haven't given themselves enough
                  time.

                  > The code you gave me works fine btw, so thank you very
                  > much for that.

                  Glad to hear that. Variables certainly *could* be used to store the
                  before-this-character sub-string and the after-this-character sub-string,
                  but variables merely represent expressions, so the bare expressions
                  themselves work just as well.


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