?
Solved

Coldfusion: performing a replace on a portion of a string

Posted on 2007-07-19
4
Medium Priority
?
204 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.
0
Comment
Question by:paddycobbett
  • 2
4 Comments
 
LVL 24

Expert Comment

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

0
 
LVL 1

Author Comment

by:paddycobbett
ID: 19526936
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
 
LVL 14

Accepted Solution

by:
Scott Bennett earned 2000 total points
ID: 19527373
<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
 
LVL 1

Author Comment

by:paddycobbett
ID: 19527485
That did the trick, thanks alot!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

840 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