3 Replies Latest reply on Mar 7, 2012 2:26 AM by BKBK

    Replace() question?

    WestSide Level 1

      Hi,

       

      I'm running ColdFusion 9.  I have a few strings like this:

       

      50;#Rick Smits

       

      20;#John Doe

       

      20;#Jane Doe

       

      20;#Jack Smith

       

      How can I remove the pound sign (#) and everything to the left.  The strings aren't always the same except for the ";#" part so I can't hardcode it.

       

      Any help appreciated.


      Thanks,

        • 1. Re: Replace() question?
          BKBK Adobe Community Professional & MVP

          You could ask ColdFusion to consider the string a list with delimiter #, and to pick out the second list element.

           

          <cfset testString = "50;##Rick Smits">

          <cfoutput>#listgetat(testString,2,"##")#</cfoutput>

           

          Notice that I have escaped ColdFusion's special symbol # with another #.

          • 2. Re: Replace() question?
            Adam Cameron. Level 5

            There's perhaps two ways of reading the requirement.  I read it as "remove and get rid..." whereas you're reading it as "remove and return".

             

            In case it's the way I'm reading it, my solution would be similar to yours, except using listFirst() to just grabt he first bit, and discarding the rest.

             

            If it was to "remove and return", I'd perhaps use listRest() which - to me, and in a pedantic way - is closer to what the person might be wanting: "the rest of the string after the first #", not "the second element in a #-delimited list".  Obviously given the sample data it amounts to the same thing in this case.

             

            Just demonstrates there's more than one way to skin a cat...

             

            --

            Adam

            • 3. Re: Replace() question?
              BKBK Adobe Community Professional & MVP

              Adam Cameron. wrote:

               

              There's perhaps two ways of reading the requirement.  I read it as "remove and get rid..." whereas you're reading it as "remove and return".

               

              In case it's the way I'm reading it, my solution would be similar to yours, except using listFirst() to just grabt he first bit, and discarding the rest.

               

              If it was to "remove and return", I'd perhaps use listRest() which - to me, and in a pedantic way - is closer to what the person might be wanting: "the rest of the string after the first #", not "the second element in a #-delimited list".  Obviously given the sample data it amounts to the same thing in this case.

               

              Just demonstrates there's more than one way to skin a cat...

               

              The more the merrier. Here is another:

               

              <cfset testString = "50;##Rick Smits">

              <cfoutput>#listLast(testString,"##")#</cfoutput>