CFLib.org – Common Function Library Project

MaxLength(string, length)

Last updated February 24, 2002

author

John Reed

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Limits the length of string output to a specified length and appends "..." to indicate the string has been trimmed. Ideal for limiting a string's length in a table without wrapping.

Return Values:
Returns a string.

Example:

<cfset tmp = "This a short sample string.">
<cfoutput>
    #maxLength(tmp, 12)#
</cfoutput>

Parameters:

Name Description Required
string The string to modify. Yes
length The max length to use. Yes

Full UDF Source:

/**
 * Limit a string's output to the desired length.
 * 
 * @param string 	 The string to modify. 
 * @param length 	 The max length to use. 
 * @return Returns a string. 
 * @author John Reed (johnreed1972@yahoo.com.au) 
 * @version 1, February 24, 2002 
 */
function maxLength(string, length) {
	var padding = 3;
	var tmp = "...";	
		
	if ( len(string) gte length )
		return removeChars(trim(string), length, len(string) - padding) & tmp;
	else return string;
}
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