ColdFusion String is a Java String

On July 23, 2008, in ColdFusion, by Anuj Gakhar

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 :-

Below is some sample code :-

String Comparison :-

myStr = "This is a ColdFusion String.";
myStr2 = "This is another string – just here for some tests.";

// Java has these functions for comparing strings so lets try and use them.
cmpNoCase = myStr.compareToIgnoreCase(myStr2);
cmp = myStr.compareTo(myStr2);
// if you want a boolean return then rather use equalsIgnoreCase() or equals()
eqNoCase = myStr.equalsIgnoreCase(myStr2);
eqCase = myStr.equals(myStr2);

// equivalant function in CF for comparing strings
cmpCF = CompareNoCase(myStr, myStr2);

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 :-


// concatenate one string to another
strConcat = myStr.concat(myStr2);
// CF equivalant
strConcatCF = myStr & myStr2;

String Find and Replace :-

// find the substring of a string
strSub = myStr.substring(2,11); // this means start from 2nd character and stop at 11th
//CF equivalant
strSubCF = Mid(myStr,3,9); // this means start from 3 and go on for 9 characters , so upto 12

// Find the first occurence of a string in another string
firstIndex = myStr.indexOf("ColdFusion");

// Find the last occurence of a string in another string
lastIndex = myStr.lastIndexOf("n");

// equivalant in CF
firstIndexCF = FindNoCase("ColdFusion", myStr);

// See if a String matches a regular expression or not
strMatches = myStr.matches(".+"); // tell the regex to get as greedy as it can

// equivalant in CF
//Refind() or RefindNoCase()
strMatchesCF = RefindNocase(".+",myStr);

// replace one or all occurences of a regex in a string
strReplace = myStr.replace("n" , "nn");
strReplaceAll = myStr.replaceAll(".+a","_");// replace anything upto the first a with a _
strReplaceFirst = myStr.replaceFirst(".+a","_"); // replace first occurence

//equivalant in CF
//ReplaceNoCase() or REReplaceNocase()

// returns the character at the specified index
findChr = myStr.charAt(15).toString();


Miscellaneous :-


// check if a string ends with a specified suffix.
strToCheck = ".";
strEndsWith = myStr.endsWith(strToCheck);

// in CF, you would have to do…
strEndsWithCF = fncEndsWithCF(myStr, strToCheck);
function fncEndsWithCF (arg1,arg2){
if (Right(arg1, Len(arg2)) EQ arg2)
return true;
return false;

// check if a string begins with a specified prefix
strToCheck = "This";
strBeginsWith = myStr.startsWith("This");

// in CF, you would have to do…
strBeginsWithCF = fncBeginsWithCF(myStr, strToCheck);
function fncBeginsWithCF (arg1,arg2){
if (Left(arg1, Len(arg2)) EQ arg2)
return true;
return false;

// Find the Length of a string
strLen = myStr.length();
// equivalant in CF
strLen = Len(myStr);

// make a string lowercase
strlowerCase = myStr.toLowerCase();
// in CF
strlowerCase = LCase(myStr);

// make a string uppercase
strUpperCase = myStr.toUpperCase();
// in CF

//trim a string
strTrim = myStr.trim();
// in CF
strTrim = Trim(myStr);


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.

// Splits this string around matches of the given regex
arrSplit = myStr.split("i"); //returns an array (split by the letter ‘i’)

// equivalant in CF but no regex support in delimtiers.
//loop over a list with the delimiter listlen(myStr, "Co")


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.


3 Responses to ColdFusion String is a Java String

  1. Glenn says:

    Great tip – thanks!

  2. Geoff C says:

    Also need to remember that java indexes from 0 but CF indexes from 1, so java functions like lastIndexOf() will return a value that needs +1 to be used in a CF function.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar
%d bloggers like this: