Using RegEx and Java to Split a String in Coldfusion

I was looking for a great way to split a string using a regex expression. Ben Nadel had a good post where he’s creating an RESplit() method. This was a little more than I wanted to go into, and after some more searching I found the solution was to use Java (usually is the solution ;-) as shown by Raymond Camden. So without further discussion, here’s what you need:

// Splitting a query column value by linefeeds
// You could just as easily do this with any RegEx
ItemsArray = myQuery.Notes[1].split( chr(13) );

Convert an Array to Query in Coldfusion

Look below for a little function I wrote to convert an array to a query. I had to write this to allow for my use of java.io.File.list() rather than cfdirectory due to speed limitations of the latter. More on that with benchmarks later here.

	/*
	* arrayToQuery
	* Allows us to convert an array into a single column query.
	* @author	Ryan Mueller @CreativeNotice
	* @created	10/27/2011
	* @param	{Array}		arr	Array to be converted		{required}
	* @param	{String}	colname	Name for query column	{required}	default=col1
	*/
	public Query function arrayToQuery(required array arr,required string colname='col1'){
		var qry = queryNew(arguments.colname);
		for(i=1;i <= ArrayLen(arguments.arr);i=(i+1)){
			queryAddRow(qry);
			querySetCell(qry,arguments.colname,arr[i]);
		}
		return qry;
	}