CFLib.org – Common Function Library Project

MaxLength(string, length)

Last updated February 25, 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;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson