Hidden String Functionality

You can do more with ColdFusion strings than you might realize by using the java.lang.String methods. Most of the functionality provided by java.lang.String is already available in some form or another as ColdFusion string functions, and of course, there’s a way to do just about anything you want with ColdFusion string functions, but there a few of nice java.lang.String methods which I really like being able to use in ColdFusion:

  • toCharArray()
  • endsWith()
  • startsWith()
  • getBytes()
  • lastIndexOf()

Just use them as though they were native (which I guess technically they are), like so:

str = "This is my string.";
myCharArray = str.toCharArray();
thisIsTrue = str.startsWith("T");
thisIsFalse = str.endsWith("!");
myByteArray = str.getBytes();
fourteen = str.lastIndexOf("i");

6 Responses to Hidden String Functionality

  1. Massimo,The point of the post was that you don’t need components like that. What’s the point in wrapping up functionality that’s already there?Christian

  2. string.split() is one string method I appreciate. Though without cfmx, you can use it through ListToArray(list,”,”). string.lastIndexOf() is another of my favorites, though somewhat available by reversing a string and doing a little math.Christian, i agree that having a cfc to encapsulate methods that exist without doing anything special is somewhat useless. I downloaded that CFC from cfczone.com a while ago, read what it did, then promptly deleted it.

  3. PaulH says:

    christian,anything for changing char encoding?

  4. Massimo Foti says:

    There is a very specific reason why I wrote the CFC. Accessing Java’s string methods directely is very handy, but it’s not documented so, it’s not supported either. I can’t be sure it’s going to be supported on future releases.Using a CFC I keep the functionality inside one single place and, if anything will chnage in the future I have only a single CFC to fix.Of course, this is a *very* conservative choice but, being burned in the past, I learned to be very careful using undocumented features 🙂

  5. Massimo Foti says:

    BTW ListToArray(list,”,”) isn’t a fully equivalent of string.split(), since you can pass a RegExp to split(). Since I am a RegExp freak, I find it very handy