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

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

CERTIFIED EXPERT

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

Author

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.

Ask the Experts

Author

Commented:
That did the trick, thanks alot!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

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

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.