View Full Version : Examples of using the Replace Function in ColdFusion

July 9th, 2009, 02:14 PM
Here is an example of how to use the Replace Function in ColdFusion to remove any Carriage Return (chr(10)) and Line Feeds (chr(13)) from a string.

<cfset MyString = Replace(MyString, "#chr(10)##chr(13)#", "", "ALL")>

The "ALL" indicates every instance of chr(10) and chr(13) together will be replaced. You could change the "ALL" to just "" and it would only Replace the first instance.

A better way in my opinion to handle this would be to use the REReplace Function like:

<cfset MyString="this is a #chr(13)# test and another #chr(10)# test">

This example will produce the following results, when you look at the page source:

this is a
test and another
this is a test and another test

The REReplace Function allows a regular expression to be used in the replace. By setting the regular express to "[#chr(10)#|#chr(13)#]" we are saying to look for and replace EITHER chr(10) or chr(13).

Of course, your specific application may or may not work with this example, but hopefully with the details here you can make a couple of minor adjustments to get your application working.