Coldfusion: performing a replace on a portion of a string

Posted on 2007-07-19
Last Modified: 2013-12-24
How in coldfusion would one do the following?

I have a large string and I want to replace each occurance of "<" with "{" and each occurance of ">" with "}". Now the thing is this, i do not want to perform this basic replace function on the entire string but rather on a portion of it. I only want to perform this replace function between index x and index y of the string. What is the best way to go about this?? Many thanks.
Question by:paddycobbett
    LVL 24

    Expert Comment

    string2 = left(string, x) & ReplaceList(mid(string,x+1, len(string)-y), "<,>", "{,}") & mid(string, y+1, len(string)-y+1)

    LVL 1

    Author Comment

    You seem to be the on the right track. The value I supply for x works perfectly, but the y value does not work as expected. y is the index at which the replace should stop peforming. To use this rather silly example:

    <cfset x=4 />
    <cfset y=8 />
    <cfset string="<><><><><><><>" />
    <cfset string2 = left(string, x) & ReplaceList(mid(string,x+1, len(string)-y), "<,>", "{,}") & mid(string, y+1, len(string)-y+1)/>

    Produces the output:


    When given the x value of 4 and y value of 8 I would have expected:


    Another example, with the same string but x=2 and y=6 the above code returns:


    when i'd expect:


    In both cases the code correctly starts replacing at the specified x index, but the y index isn't performing as expected. Can you see at a glance how to correct the algorithm? Many thanks for your efforts! =)
    LVL 14

    Accepted Solution

    <cfset x=4 />
    <cfset y=8 />
    <cfset string="<><><><><><><>" />
    <cfset string2 = left(string, x) & ReplaceList(mid(string,x+1, y-x), "<,>", "{,}") & right(string, len(string)-y)/>
    LVL 1

    Author Comment

    That did the trick, thanks alot!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Network it in WD Red

    There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

    PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
    Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now