Mimicking fixNewLine in ColdFusion Script

This post is more than 2 years old.

A few days ago a reader asked me if there was a way to mimic the "fixNewLine" attribute in ColdFusion script. I thought he meant addNewLine, the feature that handles adding a new line character for you when you write text to a file. Turns out this is a feature I've never even heard of. From the docs:

fixNewLine: yes: changes embedded line-ending characters in string variables to operating-system specific line endings.

no: does not change embedded line-ending characters in string variables.

I checked and there is not a way to do this via fileWrite or FileWriteLine. I whipped up the following little function that should have the same behavior.

function fixNewLine(s) {
	var isWindows = server.os.name contains "windows";
	// http://stackoverflow.com/a/6374360/52160
	if(isWindows) {
		return rereplace(s, "\r\n|\n|\r","#chr(13)##chr(10)#", "all");
	} else {
		return rereplace(s, "\r\n|\n|\r","#chr(10)#", "all");

The code is based on a StackOverflow answer (I credited the link in the code) and just does a simple regex on the string based on the operating system. You could also modify this to force a style instead of sniffing the OS.

Any thoughts on this? I did some basic testing and it seems to work OK. If folks like this I'll put it up on CFLib.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Peter Boughton posted on 2/10/2014 at 10:10 PM

Here's a better version:

function fixNewLine(s)
return s.replaceAll('\r(?!\n)','\n').replaceAll('\r','')

It removes all carriage returns in all situations, because they're a waste of space and rarely actually required. The sooner they are forgotten as a quirk of computing history and stop causing unnecessary extra code, the better.

Comment 2 by Raymond Camden posted on 2/10/2014 at 10:22 PM

Better being a point of view. ;) In my case, I'd disagree since the point was to replicate the behavior - right or wrong.

Comment 3 by Phillip Senn posted on 2/10/2014 at 10:43 PM

I've been thinking about your encouragement to post into ColdFusion UI, the right way. and one of the reasons I'm hesitating is because I have a unique point of view on how to write everything in both ColdFusion and JavaScript. I'm sure that if I were to post a "chapter" in ColdFusion UI the right way, both you and Adam would ask "Why do you do it that way?" and would proceed to correct it.

I touched on this as a comment to an earlier blog post of yours, so the irony is not lost.
I guess what I'm saying is that I think it's great that you and Adam are publishing "The right way" according to your point of view, and I'm watching that github project closely.

For me to show an example, I would want to have control over the editing.
So: I guess, maybe, I need to change my attitude?

Comment 4 by Raymond Camden posted on 2/10/2014 at 10:49 PM

Phillip, these are valid questions, but can I ask why you didn't post it on the blog entry? This comment is really OT here and I'd suggest you post it there.

Comment 5 by Phillip Senn posted on 2/10/2014 at 10:51 PM

Oh, it's because you said that "Better being a point of view" in this blog post, but sure, I will post it to the other.

Comment 6 by Cutter posted on 2/11/2014 at 12:52 AM

How about:

function fixNewLine(s) {
return rereplace(s, "\r\n|\n|\r", ((server.os.name contains "windows") ? chr(13) : "") & chr(10), "all");

Would that work?

Comment 7 by Raymond Camden posted on 2/11/2014 at 2:08 AM

Yes, but only if you want to be fancy and shit. ;)

Comment 8 by Edward Beckett posted on 2/16/2014 at 5:48 AM

What about?

ls = createObject("java", "java.lang.System").getProperty("line.separator");

Comment 9 by Raymond Camden posted on 2/18/2014 at 7:56 AM

That doesn't do what the UDF does - it just tells you what the separator is.