• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Coldfusion: performing a replace on a portion of a string

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.
0
paddycobbett
Asked:
paddycobbett
  • 2
1 Solution
 
mankowitzCommented:
string2 = left(string, x) & ReplaceList(mid(string,x+1, len(string)-y), "<,>", "{,}") & mid(string, y+1, len(string)-y+1)

0
 
paddycobbettAuthor 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! =)
0
 
Scott BennettManager TechnologyCommented:
<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>
0
 
paddycobbettAuthor Commented:
That did the trick, thanks alot!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now