CFLib.org – Common Function Library Project

format(str, args)

Last updated March 10, 2013

author

Anthony Cole

Version: 1 | Requires: CF9 | Library: StrLib

Description:
CFML-friendly wrapper for a call to Java String's format() function. You can read more about the syntax on Oracle's site: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

Return Values:
A formatted string

Example:

format("%s is a required field", ["email"])

Explicit argument indices may be used to re-order output.
format("My last name is %2$s and my first name is %1$s", ["john","doe"])

Parameters:

Name Description Required
str A format string Yes
args Array of arguments referenced by the format specifiers in the format string. Yes

Full UDF Source:

/**
 * Uses the Java String format() method to format a string in CFML.
 * v0.9 by Anthony Cole
 * v1.0 by Adam Cameron (renamed from sprintf to format, as this more closely reflects the underlying implementation; improved argument/variable names and tweaked the logic slightly)
 * 
 * @param str 	 A format string (Required)
 * @param args 	 Array of arguments referenced by the format specifiers in the format string. (Required)
 * @return A formatted string 
 * @author Anthony Cole (acole76@gmail.com) 
 * @version 1.0, March 10, 2013 
 */
string function format(required string str, required array args){
	return str.format(str, args);
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson