5 Replies Latest reply on May 20, 2010 11:20 AM by GordonSmith

    Remove spaces from a variable


      I have a variable called 'enteredname':-




      var enteredname:String;


      When the user enters some text into my text input 'nameentry' and clicks a button the text entered is saved into this variable:-


      enteredname = nameentry.text;


      This variable is then sent to a server and set as the name of the item the user has added via http request however I want to add in another string variable 'enteredimagename' which will store the same text but will have any spaces removed or any characters that would not be appropriate to an image/jpeg file.


      The idea of this is that the user adds a name for an item their adding and the same name gets applied both as the items title and as the items image filename (with all the spaces etc removed) as all of the images in my app are stored on a server and are dynamic so the click would look something like:-


      enteredname = nameentry.text;

      enteredimagename = nameentry.text (with some code to remove the spaces for this string or something)


      Can anyone help me out with this?

        • 1. Re: Remove spaces from a variable
          Sebastien V. Level 3

          To remove whitespaces from a string:


          var enteredImageName:String = StringUtil.trim( nameentry.text );


          Edit: my bad, this only removes whitespaces at the beginning or the end of the String

          1 person found this helpful
          • 2. Re: Remove spaces from a variable
            BhaskerChari Level 4

            Hi use this function pass the name or any string that you want to remove strings..the function will return you the concatenated string without spaces.


            private function stripSpaces():String
                var strippedSpaces:String = StringUtil.trim(name);
                var strArray:Array = strippedSpaces.split(" ");
                var concatString:String = "";
                for(var i:int=0;i<strArray.length;i++)
                 if(strArray[i] != " ")
                  concatString += strArray[i];
                return concatString;   


            This function removes all spaces, also the spaces between words and at the beginning and end as well.


            If this post answers your question or helps, please kindly mark it as such.



            Bhasker Chari

            1 person found this helpful
            • 3. Re: Remove spaces from a variable
              jimmyoneshot Level 1

              Cheers guys. Both of those should work fine. Just while I'm here do any of you know what sort of characters would stop a jpeg from displaying properly in flex? What I mean is this the enteredimagename will be uploaded as the name of jpegs that are saved on my server. I have many images in my app that have these online jpegs as their sources sort I'm wondering what characters I'll have to get rid of or restrict so that they display correctly?

              • 4. Re: Remove spaces from a variable
                Sebastien V. Level 3

                I don't know what characters you can use in the file name, but you can always restrict it to A-Z and a-z to be sure.

                • 5. Re: Remove spaces from a variable
                  GordonSmith Level 4

                  I'm not sure what the legal characters are for loadable images. But the fastest way to remove spaces from a String is probably something like


                  str.replace(/ /g, "");


                  Gordon Smith

                  Adobe Flex SDK Team