4 Replies Latest reply on Mar 30, 2009 3:38 PM by Newsgroup_User

    Easy method for deleting chunks of strings

    Level 7
      What's the best method to delete a chunk out of a long string? The best I
      can come up with is the extremely clunky:

      str = "ABCDEFG"
      newstr = str.char[1..3] & str.char[4..7]
      str = newstr
      --"ABCEFG"

      Fine when it's just 1 character, but say I want to delete a larger chunk, or
      use the "item", "word", "line" parameters, or I don't know in advance where
      in the string the text to be deleted is. Then I have to get funky:

      on deleteChunk str, piece
      start = str.getOne(piece)
      len = piece.char.count
      newstr = str.char[1..start-1] & str.char[start+len+1..str.char.count]
      return newstr
      end

      Ugly, clunky, and error prone. What if the chunk to delete is at the
      beginning or end of the string? What if it's not there at all? I have to
      add several more lines of error-checking to make this work, and even then it
      feels like a hack. It seems to me that some permutation of the "delete",
      "deleteAt", or "deleteOne" functions should work, but I can't come up with
      anything that doesn't give an error. Is there something I'm missing? These
      seem to only work on Lists. But in most languages I've used, a String is in
      essence nothing but a List of Chars. Presumably there should be a way to
      treat it this way?


        • 1. Re: Easy method for deleting chunks of strings
          Level 7
          Never mind. Stupid completely backwards logic. I'm expecting something
          like:

          str.char[1..4].delete()

          and instead it's

          delete str.char[1..4]

          Looks like a holdover from verbose syntax. Also not very well documented
          for some reason. Nowhere in the documentation for the "delete" functions
          does it say it works on strings, and it does, but with completely reversed
          syntax. Go figure. Also doesn't say it anywhere obvious under the Strings
          documentation. I only found it by looking at the obsolete "chars" function.
          Why would something so basic be so hidden?


          • 2. Re: Easy method for deleting chunks of strings
            Level 7
            Or you can be even more verbose and do this:

            put EMPTY into str.char[1..4]
            • 3. Re: Easy method for deleting chunks of strings
              Level 7
              Wow, I remember that vintage coding method ;)



              --
              Craig S. Wollman

              Word of Mouth Productions
              159-00 Riverside Drive West
              New York, New York 10032
              (212) 928-9581
              "Mike Blaustein" <mblaustein@gmail.com> wrote in message
              news:gqraa0$b84$1@forums.macromedia.com...
              > Or you can be even more verbose and do this:
              >
              > put EMPTY into str.char[1..4]


              • 4. Re: Easy method for deleting chunks of strings
                Level 7
                > Or you can be even more verbose and do this:
                >
                > put EMPTY into str.char[1..4]

                What, no "put EMPTY into char[1..4] of str"? Actually, I think it gets even
                worse than that, because I don't think the "char[1..4]" construct existed
                back in the early days, but I've spent years forcefully expunging all traces
                of verbose from my head - it always annoys me when I run into something that
                can't be written in dot-syntax.

                The other one that bothers me frequently is that there doesn't seem to be a
                dot-syntax way of using the "last" property. If I want the last character
                in a string, I have to do something like this:

                put str.char[str.char.count]

                The verbose method looks like:

                put the last char of str

                According to the help files, you can use the slightly less clumsy:

                put last char(str)

                But here the documentation is just flat out lying, because that doesn't work
                at all. Just results in an error every time. I'd like to see something
                like:

                put str.char.last() or put str.char[last]

                But no dice. The old, old verbose style is the only one that works, other
                than my original, somewhat redundant dot-syntax example, which can get
                really hairy when "str" is not just a single variable, e.g.:

                put
                sprite(me.spriteNum).member.name.char[sprite(me.spriteNum).member.name.char.count]

                Yuck.