Another option for trimming text in ColdFusion

Yesterday I wrote a blog entry discussing how you can trim text in ColdFusion without cutting words off in the middle. I mainly focused on the handy udf FullLeft(), which made the task easier. Today I was in a church service (my son’s school was closing today - don’t worry - I haven’t turned pious all of a sudden) and noticed something odd. On the bulletin for the program, they listed some text with a split like so:

Insert some bible text here. I guess I'm a sucky Catholic as I can't remember any bible verses. ... The end of the text is here. Blah blah lorem ipsum deloras.

Notice that instead of doing “Left text…”, they trimmed the middle of the text. Seeing this I immediately though - I can do this in ColdFusion! It turned out to be rather easy. First let’s start with a new quote:

<cfsavecontent variable="quote"> So here I was, home again after all those years. Standing in the main square (which I had crossed countless times as a child, as a boy, as a young man), I felt no emotion whatsoever; all I could think was that the flat space, with the spire of the town hall (like a soldier in an ancient helmet) rising above the rooftops, looked like a huge parade ground and that the military past of the Moravian town, once a bastion against Magvar and Turk invaders, had engraded an irrevocable ugliness on it's face. </cfsavecontent>

(Taken from the first page of “The Joke”.) What I figured I’d do is grab a fullLeft from, well, the left, and do a FullRight for the end of the text. There is no FullRight, so what I tried instead was:

<cfset test = reverse(fullLeft(reverse(quote), 200))>

Which worked like a charm! I basically reversed the text, did a fullLeft, and reversed the result. The complete code example (minus the cfsavecontent) is below:

<cfoutput> #fullleft(quote, 200)#<br /> ... <br /> </cfoutput> <cfset test = reverse(fullLeft(reverse(quote), 200))> <cfoutput>#test#</cfoutput>

The final result:

So here I was, home again after all those years. Standing in the main square (which I had crossed countless times as a child, as a boy, as a young man), I felt no emotion whatsoever; all I could
...
the rooftops, looked like a huge parade ground and that the military past of the Moravian town, once a bastion against Magvar and Turk invaders, had engraded an irrevocable ugliness on it's face.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless 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

Comments