ColdFusion String is a Java String

ColdFusion strings are Java Strings internally, which means they are an Object of class “java.lang.String” . In this post, I will try and use some of these Java String functions that can be applied directly to a ColdFusion string variable. I understand that most of these have been discussed in bits and pieces all over the blogosphere but I am just building up a cumulative list of these functions.

The functions that I tried are below :-
.compareTo()
.compareToIgnoreCase()
.equals()
.equalsIgnoreCase()
.charAt()
.concat()
.endsWith()
.startsWith()
.indexOf()
.lastIndexOf()
.length()
.matches()
.split()
.substring()
.toLowerCase()
.toUpperCase()
.trim()

Below is some sample code :-

String Comparison :-

What I noted is that, the ColdFusion CompareNoCase() returns -1 if the strings dont match whereas the Java compareTo() returns the actual difference the strings (-78 in the above example).

String Concatenation :-

String Find and Replace :-

Miscellaneous :-

And this one is my favorite, by far. The Split() function. The only way to do this in native CF would be using List functions with delimiters.

So, as you can see, there is a completely different way of doing String manipulations if we use the underlying Java functions. It makes it much more flexible and easier to write. However, not sure about the compatibility with BlueDragon or Railo as I havent tested this code on any of those. But as long as a String is an Object of java.lang.String, this all should work, in theory.

Comments:

2 Responses to “ColdFusion String is a Java String”


  1. Great tip – thanks!


  2. Thanks Glenn.:)

Leave a Reply