I don’t know if this is new to everyone, but it was to me. Jason Delmore , the Product Manager for ColdFusion informed us of something intriguing in his Developing Hybrid Applications with ColdFusion and Java and talk today. He informed us that ColdFusion strings are an extension of the Java String class. Which means that methods of Java strings are available to ColdFusion. What does this mean? It means that this is valid code:
<cfset test_1= "A string to test" />
<cfset test_2= "A string to add on to a string to test." />
<cfset test_1 = test_1.concat(test_2) />
Not only is it more elegant of other ColdFusion string concatenation techniques, it appears to run about twice as fast.
Also any other method listed in the Java String reference will work.
7 thoughts on “ColdFusion String Manipulation”
Yeah that is cool. Also, perhaps my favorite Java string method for use in ColdFusion is ReplaceAll() and ReplaceFirst(). These use regular expressions and are much faster than the CF equivalents.
Don’t forget about StringBuffers, they perform very well especially when building large strings.
I haven’t compared this method to StringBuffers yet. So much to try, so little time.
I’ve always liked the string method .startsWith() much easier to use.
Let’s not also forget –
Parsed Dates are java.util.Date
Arrays are java.util.Vector
Structs are java.util.Hashmap
Query results are java.sql.ResultSet
there are lots and lots out there ;o)
Yes, JAlpino is correct. If you need to do much string manipulation, use StringBuffers. Strings in Java are immutable objects, so at least one new object must be created when you “change” a String.
I subtly diagree with you, Brandon. I would say if you have to optimize string manipulation, use StringBuffers.
Great! I only recently started using Cold Fusion, and was just doing some research on it. It’s good to know that ReplaceAll(); will work.