Skip to main content
Inspiring
March 6, 2012
Question

Replace() question?

  • March 6, 2012
  • 1 reply
  • 684 views

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,

This topic has been closed for replies.

1 reply

BKBK
Community Expert
Community Expert
March 7, 2012

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 #.

Inspiring
March 7, 2012

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

BKBK
Community Expert
Community Expert
March 7, 2012

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>