3 Replies Latest reply on Mar 2, 2008 8:04 AM by GerryIsHere

    Removing those f...... commas !!!

    GerryIsHere Level 1
      Hi there!

      I'm really fed up by this:
      I have a list of the issues of a magazine designated by their issue numbers.
      Those numbers are separated by a space.
      But sometimes, users enter two spaces (or more) instead of one. They even enter spaces at the beginning or at the end of the list!

      So, the list may be:
      "-5---9-11--23---41-42--54--" (dashes represent the spaces, because the forum automatically removes the consecutive spaces!)

      I did this:
      var mylist:String = _root.packIssueListFld.text.split(" ");

      But the result I get is:
      ",,5,,,9,,,,11,,,23,,,,41,,42,,54,,," (I haven't really count the commas...)

      How can I get a clean list like this:
      "5,9,11,23,41,42,54"

      Hope somebody could help me to clean up this mess, I thank her/him in advance for!

      Best regards,
      Gerry on the edge
        • 1. Re: Removing those f...... commas !!!
          Level 7
          GerryIsHere,

          > I did this:
          > var mylist:String = _root.packIssueListFld.text.split(" ");

          That creates an array (a list) out of the characters in the string,
          using the space character as a delimiter. What that means is that every
          time ActionScript sees a space character, it counts that as a new item in
          the array.

          > But the result I get is:
          > ",,5,,,9,,,,11,,,23,,,,41,,42,,54,,," (I haven't really count the
          > commas...)

          That makes perfect sense. Looking at your original string:

          5 9 11 23 41 42 54

          ... if you had split that string on the "4" character instead ...

          _root.packIssueListFld.text.split(" ");

          ... you would have gotten an array with three elements:

          5 9 11 23 ,1 ,2 5,

          That's an array with four elements in it:

          a) 5 9 11 23
          b) 1
          c) 2 5
          d) (two empty spaces)

          Note that the 4s from the original string are now missing, because those
          are the characters that were used to create each new element of the array.
          In your case, each new element is created every time a space is encountered.
          In any case, the String.split() method returns an array; in your code,
          you're asking it to return a string.

          > How can I get a clean list like this:
          > "5,9,11,23,41,42,54"

          For that, you need to get rid of the spaces. You could use a for() loop
          for that, stepping through the each character in the original string, and
          skipping past the spaces. You don't really need to go through an array at
          all. Here's one way to do it.

          ==============================

          var originalString:String = "5 9 11 23 41 42 54 ";

          var newString:String = "";
          var currentCharacter:String = "";
          var lastCharacter:String = "";

          var len:Number = originalString.length;
          for (var n:Number = 0; n < len; n++) {
          currentCharacter = originalString.charAt(n);
          if (currentCharacter != " ") {
          newString += currentCharacter;
          } else {
          if (lastCharacter != currentCharacter) {
          newString += ",";
          }
          }
          lastCharacter = currentCharacter;
          }
          if (newString.charAt(newString.length - 1) == ",") {
          newString = newString.substr(0, newString.length - 1);
          }

          trace(newString);

          ==============================

          That may seem like a lot of code, but it works just fine. There are
          certainly other ways to accomplish what you're after, and others may well
          jump on this thread and suggest a more succinct approach. I tried to
          arrange this so that the logic is relatively easy to step through. The
          for() loop basically steps through each character in the original string,
          one at a time. If that character is NOT a space, it adds that character to
          the new string ... else (meaning, if it is a space) it checks if the current
          character is the same as the last one it checked (because you want to skip
          multiple adjacent spaces). If it is NOT the same as last time, it adds a
          comma to the new string. Finally, it updates the last character checked to
          the current one.


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


          • 2. Re: Removing those f...... commas !!!
            w m d
            Not sure if you want to end up with an array or a string, but here's a way to get rid of the spaces
            • 3. Re: Removing those f...... commas !!!
              GerryIsHere Level 1
              Dear Professor David,

              So much devotion leaves me speech and breathless!
              I thank you very much not only for the code but foremost for the detailed and crystal clear explanations.

              And...

              I thank you too, w m d, for your nice piece of code!

              Problem is now: which one to choose?
              No, I'm joking! The one I choose will remain secret!
              :-)

              Best regards,
              Gerry