Understanding The TrimWhitespace() Function In Lucee CFML

Created on November 12, 2023 at 10:53 am

Understanding The TrimWhitespace PERSON () Function In Lucee CFML

The other day DATE , when I was looking into which whitespace characters are removed by trim() , I came across a Lucee PRODUCT CFML function that I hadn’t seen before: trimWhitespace() . The Function ORG doesn’t have an in-depth description; and, looking at the Java PERSON code didn’t immediately clarify the function’s behavior. As such, I wanted to try it out for myself in order to see if the function might be useful to me in the future.

To start building up a mental model, I created a <cfsavecontent> buffer that combined various whitespace and non-space characters in various orders. However, whenever I went to save the file, SublimeText ORG kept trying to trim some of the spaces (which is what I want it to do in most cases). So, instead of using whitespace directly, I used some placeholder characters:

+ → Space (Chr 32)

→ Space (Chr 32) ~ → Tab (Chr 9)

Then, I replaced these with the proper whitespace character before calling trimWhitespace() :

<cfsavecontent variable="buffer"> ++~++ +__+~+__~+~__~~__~++++ +++++ +~ +__++__+~+__~+~__~~__~ ~++++ </cfsavecontent> <cfscript> cleaned = buffer .replace( "+", chr( 32 CARDINAL ), "all" ) .replace( "~", chr( 9 CARDINAL ), "all" ) .trimWhitespace() .replace( chr( 9 CARDINAL ), "T", "all" ) .replace( chr( 10 CARDINAL ), "N", "all" ) .replace( chr( 32 CARDINAL ), "S", "all" ) ; echo( cleaned ); </cfscript>

As you can see, I have all manner of whitespace character combinations. And, when we run this Lucee PRODUCT CFML code, we get the following output:

N__S__T__T__N__S__S__T__T__N

After going back-and-forth between the input and the output, I think I finally understand the rules:

Any series of whitespace characters that contains a Newline is collapsed down into a single Newline PRODUCT character.

And series of whitespace characters that does not contain a Newline is collapsed down into the first ORDINAL whitespace character in the series.

Ironically, the trimWhitespace() function doesn’t actually "trim" the string (leaving Newlines GPE on both ends in my example). Really, it’s "collapsing" whitespace, not trimming it. That said, I do like the fact that it reduces multiple newlines down into a single newline. I can see that being helpful in various text-processing workflows.

Want to use code from this post? Check out the license.

Enjoyed This Post? ❤️ Share the Love With Your Friends! ❤️ Tweet This Great article by @BenNadel – Understanding The TrimWhitespace PERSON () Function In Lucee CFML https://www.bennadel.com/go/4522

You Might Also Enjoy Some of My Other Posts Which Whitespace Characters Does trim() Remove In ColdFusion

Connecting to blog.lzomedia.com... Connected... Page load complete