3 Replies Latest reply on Aug 7, 2010 10:47 PM by Andrei1

    Remove spaces in text variable

    Chemistry guy Level 1

      I'm slowly converting my older Director-made tutorials to Flash. The user inputs text, and then I evaluate it. Before this evaluation, I want to remove spaces to avoid the user's input being counted incorrect because of an extra space. How do I do this for Flash CS5?

       

      Here's what I did for Director.

       

      TermNoSpaces = gTerm

       

       

       

      spaceChar = numToChar(32) --routine to remove spaces from the user input

       

       

       

      if TermNoSpaces contains spaceChar then

           repeat while TermNoSpaces contains spacChar

                lessSpace = offset(spaceChar, TermNoSpaces)

                delete TermNoSpaces.char[lessSpace]

                put TermNoSpaces

           end repeat

      end if

       

       

       

       

       

        • 1. Re: Remove spaces in text variable
          Deepanjan Das Level 3

          Hi,

          Use trim function:

           

          /**
          * Removes any extra spaces before and after a String
          *
          * @param: value:String – value to be trimmed
          * @return: String value – the trimmed value
          */
          public function trim(value:String):String{
          var j:uint = 0;
          while (value.charAt(j) == ” “){
          j++
          }
          value = value.substring(j);
          var k:uint = value.length – 1;
          while(value.charAt(k) == ” “){
          k–
          }
          value = value.substring(0, k+1)
          return value;
          }

           

          Hope this helps.

           

          Warm Regards

          Deepanjan Das

          http://deepanjandas.wordpress.com/

          • 2. Re: Remove spaces in text variable
            Andrei1 Level 6

            The best approach to manipulate strings is using regular expressions.

             

            This code trims string off spaces on both ends:

             

            var str:String = " this is string ";
            var trim:RegExp = /^ +| +$/g;
            str = str.replace(trim, "");
            trace("BEGIN" + str + "END");
            

             

            This eliminates all the spaces:

             

            var str:String = " this is string ";
            var space:RegExp = /\s/g;
            str = str.replace(space, "");
            trace(str);
            

             

            This code replaces multiple spaces with a single space:

             

            var str:String = "this   string      with       many       spaces";
            var singleSpace:RegExp = /\s{2,}/g;
            str = str.replace(singleSpace, " ");
            trace(str);
            
            • 3. Re: Remove spaces in text variable
              Andrei1 Level 6

              Actually I think this is more correct:

               

              var trim:RegExp = /^\s+|\s+$/g;
              var str:String = "      this is string ";
              str = str.replace(trim, "");
              trace("BEGIN" + str + "END");