CFLib.org – Common Function Library Project

midLimit(sString, nLimit)

Last updated May 9, 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;
}
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