• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Replace() question?

Engaged ,
Mar 06, 2012 Mar 06, 2012

Copy link to clipboard

Copied

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,

TOPICS
Advanced techniques

Views

586

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 06, 2012 Mar 06, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 07, 2012 Mar 07, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 07, 2012 Mar 07, 2012

Copy link to clipboard

Copied

LATEST

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation