Remove carriage returns from a string

Quite often I need to remove carriage returns and usually I use the following code:

Replace(myvar,”#chr(13)#”,””,”ALL”);

However I have found this doesn’t always remove the carriage returns.

How did I fix it? I used the following code:

REReplace(myvar,”#chr(13)#|#chr(9)#|\n|\r”,””,”ALL”);

This works everytime, its very handy for writing html with javascript

for example:

<cfsavecontent variable=”thecode”>
<cfinclude template=”#dynamicCFMfile#”>
</cfsavecontent>

<cfscript>
newDefaultCode = Replace(thecode ,””””,”\”””,”ALL”);
newDefaultCode = REReplace(newDefaultCode ,”#chr(13)#|#chr(9)#|\n|\r”,””,”ALL”);
</cfscript>
<cfoutput>theHTML = “#newDefaultCode#”;</cfoutput>

document.write(theHTML);

This allows me to dynamically write a coldfusion template to the page via javascript.

About these ads

April 10, 2008. Tags: , , , , . coldfusion, Javascript.

5 Comments

  1. Steven Benjamin replied:

    Thank you for posting this.

  2. msingh replied:

    Amazing…you just saved me a bottle of scotch…I owe you one.

  3. damiththa replied:

    this is a the best, finally found the solution for bugging me for days, literally days.
    Thank you, works like a charm.

  4. Frank Tudor replied:

    Thanks for this post!!! Dynamic string creation over here is fails…Your snippet fixed the problem…

  5. Brant Kelsey replied:

    I had better luck with:

    REReplace(myvar,”#chr(13)#|#chr(9)#|\n|\r”,””,”ALL”);

    …like the example you gave below.

    #chr(9)# was missing from the upper snippet.

    Regardless, this works like a charm! THANK YOU!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback URI

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: