CF doesn't have a built-in to do it, but it's not hard to write:

function replaceAt(string, index, char) {
        if (index EQ 1) {
                return char & removeChars(string, 1, 1);
        } else {
                return left(string, index - 1) & char & removeChars(string, 1, 
index);
        }
}

Here's a test case:

<cfloop from="1" to="25" index="i">
        <cfset s = repeatString("X", 9) />
        <cfset n = randRange(1, len(s)) />
        <code>#s# : #n# : #replaceAt(s, n, "_")#</code><br />
</cfloop>

cheers,
barneyb

On Wed, Nov 5, 2008 at 12:33 PM, Andy Matthews <[EMAIL PROTECTED]> wrote:
> How might I go about replacing a specific character in a string with another 
> character? I'd like to get a random position in a string, then replace that 
> specific character with something else.
>
> So I might start with: 'XXXXXXXXXX' (10 characters)
> I'd get a random position in that string (say 7).
> Then I'd replace the X at position 7 with a $ giving me this: 'XXXXXX$XXX'
>
> Does CF have a "replaceAt" method?
>
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to 
date
Get the Free Trial
http://ad.doubleclick.net/clk;207172674;29440083;f

Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:314881
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to