We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# Coldfusion: performing a replace on a portion of a string

on
Medium Priority
228 Views
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.
Comment
Watch Question

## View Solution Only

CERTIFIED EXPERT

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

Commented:
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)/>
<cfoutput>#string2#<br></cfoutput>

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! =)
Manager Technology
Commented:
<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)/>
<cfoutput>#string2#<br></cfoutput>

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
That did the trick, thanks alot!
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile