« New Tools For MXNA | Main | CFCs vs. UDFs »
February 04, 2004
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:
<cfscript>
str = "This is my string.";
myCharArray = str.toCharArray();
thisIsTrue = str.startsWith("T");
thisIsFalse = str.endsWith("!");
myByteArray = str.getBytes();
fourteen = str.lastIndexOf("i");
</cfscript>
Posted by cantrell at February 4, 2004 12:00 PM | References
Comments
Check:
http://www.cfczone.org/cfcs/index.cfm
http://www.cfczone.org/cfcs/downloads/tmt_java_string.zip
Posted by: Massimo Foti at February 4, 2004 12:07 PM
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
Posted by: Christian Cantrell at February 4, 2004 02:41 PM
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.
Posted by: Nathan Strutz at February 4, 2004 06:47 PM
christian,
anything for changing char encoding?
Posted by: PaulH at February 4, 2004 09:41 PM
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 :-)
Posted by: Massimo Foti at February 5, 2004 05:28 AM
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
Posted by: Massimo Foti at February 5, 2004 05:32 AM