5 Replies Latest reply on Oct 4, 2010 11:59 PM by BhaskerChari

    String manipulation

    bryant_16 Level 1

      Hey guys,

       

      I have a string which is "1,2,3,4,5,6,7,8,9,10"

       

      How do I make the string into "1 2,3 4,5 6,7 8,9 10" ?

       

      Meaning to say between the first 2, there will be a space in between followed by a "," after that space between 3 and 4 and then a ","

       

      How do I achieve that?    

        • 1. Re: String manipulation
          BhaskerChari Level 4

          Hi bryant,

           

          You can use the below function to manipulate the String.

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
          <mx:Script>
            <![CDATA[
              
               private function init() : void
               {
                  var strNumbers:String = "1,2,3,4,5,6,7,8,9,10";
                  var strSplitArray:Array = strNumbers.split(",");
                  var strModified:String = "";
                 
                  var counter:int=1;
                  for(var i:int=0;i<strSplitArray.length;i++)
                  {
                   if(counter == 1)
                   {
                    strModified += strSplitArray[i] + " ";
                   }
                   else if(counter == 2)
                   {
                    strModified += strSplitArray[i] + ",";
                    counter = 0;
                   }
                   else
                   {
                    counter = 0;
                   }
                   counter++;
                  }
                  strModified = strModified.substring(0, strModified.length-1);
               }

            ]]>
          </mx:Script>
          </mx:Application>

           

           

           

           

           

          Thanks,

          Bhasker

          • 2. Re: String manipulation
            bryant_16 Level 1

            Hi there,

             

            Thanks for the prompt reply.

             

            Can I check with you one more thing? How do I make it into "2.10 1.10, 4.10 3.10, 6.10 5.10, 8.10 7.10, 10.10 9.10" from "1.10,2.10,3.10,4.10,5.10,6.10,7.10,8.10,9.10,10.10" ??

             

            I tried reversing but I will get things like this "01.2 01.1, 01.4 01.3... " something like that, which means it has reversed everything in it.

             

            I just want to reverse the position, thats all.

             

            Can help out all this?         

            • 3. Re: String manipulation
              BhaskerChari Level 4

              Hi Bryant,

               

              You can use this function instead..

               

               

              private function init() : void
                   {
                      var strNumbers:String = "1.10,2.10,3.10,4.10,5.10,6.10,7.10,8.10,9.10,10.10";

                      var strSplitArray:Array = strNumbers.split(",");
                      var strModified:String = "";
                     
                      var counter:int=1;
                      for(var i:int=0;i<strSplitArray.length;i++)
                      {
                       if(counter == 1)
                       {
                        ++i;
                        strModified += strSplitArray[i] + " ";
                        i-=2;
                       }
                       else
                       {
                        strModified += strSplitArray[i] + ",";
                        counter = 0;
                        i+=1;
                       }
                       counter++;
                      }
                      strModified = strModified.substring(0, strModified.length-1);
                   }

               

               

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


              Thanks,

              Bhasker Chari

              • 4. Re: String manipulation
                bryant_16 Level 1

                Hi there,

                 

                Thanks again for your prompt reply.

                 

                Now that if I want to change it from  "1.10,2.10,3.10,4.10,5.10,6.10,7.10,8.10,9.10,10.10" to become "2.10 1.10.10, 4.10 3.10, 6.10 5.10, 8.10 7.10, 10.10 9.10"

                 

                How do I make the first entry appear at the last entry again? Meaning to say become like this, "2 1, 4 3, 6 5, 8 7, 10 9, 2 1"

                 

                Something like that.

                 

                Appreciate your response

                • 5. Re: String manipulation
                  BhaskerChari Level 4

                  Hi bryant,

                   

                  Firstly Can you please mark it as answered for this post.

                   

                   

                  Use this function now...

                   

                  private function init() : void
                       {
                          var strNumbers:String = "1.10,2.10,3.10,4.10,5.10,6.10,7.10,8.10,9.10,10.10";
                          var strSplitArray:Array = strNumbers.split(",");
                          var strModified:String = "";
                         
                          var counter:int=1;
                          for(var i:int=0;i<strSplitArray.length;i++)
                          {
                           if(counter == 1)
                           {
                            ++i;
                            strModified += strSplitArray[i] + " ";
                            i-=2;
                           }
                           else
                           {
                            strModified += strSplitArray[i] + ",";
                            counter = 0;
                            i+=1;
                           }
                           counter++;
                          }
                          strModified += strSplitArray[1] + " " + strSplitArray[0];
                       }

                   

                   

                  Thanks,

                  Bhasker