CFLib.org – Common Function Library Project

midLimit(sString, nLimit)

Last updated May 10, 2009

author

Joshua Rountree

Version: 0 | Requires: CF5 | Library: StrLib

Description:
Limits a given string to a specified amount of characters. This UDF takes the characters from the middle of the string and inserts "..." in it's place.

Return Values:
Returns a string

Example:

<cfset TestString = "The fox ran over the log and jumped into a hole to escape the hunter.">

<cfoutput>
Original String:<br />
#TestString#<br /><br />

Limited String to 25 characters:<br />
#midLimit(TestString,25)#
</cfoutput>

Parameters:

Name Description Required
sString string to use Yes
nLimit max length of string to use Yes

Full UDF Source:

/**
 * Limits a string from the center inserting &quot;...&quot;
 * 
 * @param sString      string to use (Required)
 * @param nLimit      max length of string to use (Required)
 * @return Returns a string 
 * @author Joshua Rountree (joshua@remote-app.com) 
 * @version 0, May 9, 2009 
 */
function midLimit(sString,nLimit) {
    var nLength = Len(sString);
    var nPercent = nLimit/nLength;
    var nStart = Round(nLimit * .5);
    var nRemoveCount = (nLength - nLimit);
    var sResult = "";
    
    if(nLength GT nLimit) {
        sResult = RemoveChars(sString,nStart,nRemoveCount+3);
        sResult = Insert("...",sResult,nStart-1);
    } else {
        sResult = sString;
    }
    
    return sResult;
}

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