CFLib.org – Common Function Library Project

Indent(string[, indentchar][, repeatcount])

Last updated July 17, 2001

author

James Ang

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Return string with each line prepended with indentchar repeatcount times. This function could be useful for formatting in CFMAIL or PRE tags. A line is defined to be a string terminated by CR/LF or LF.

Return Values:
Returns a string.

Example:

<CFSAVECONTENT VARIABLE="Test">
To be or not to be, that is the question.
This is a second line of text.
And this is a third.
</CFSAVECONTENT>
<PRE>
<CFOUTPUT>
#Indent(Test)#
</CFOUTPUT>
</PRE>

Parameters:

Name Description Required
string String to be modified. Yes
indentchar Character to use for indenting. Defaults to tab character (Chr(9)). No
repeatcount Positive integer to repeat indentchar. Defaults to 1. No

Full UDF Source:

/**
 * Indents all the lines of a string.
 * 
 * @param string 	 String to be modified. 
 * @param indentchar 	 Character to use for indenting. Defaults to tab character (Chr(9)). 
 * @param repeatcount 	 Positive integer to repeat indentchar. Defaults to 1. 
 * @return Returns a string. 
 * @author James Ang (james@vertexcs.com) 
 * @version 1, July 17, 2001 
 */
function Indent(str) {
	var chri = CHR(9); // indenting character. Defaults to horizontal tab
	var ncnt = 1; // strIndent = RepeatString(ichr, ncnt)
	var strcr = CHR(13);
	var strlf = CHR(10);
	var strcrlf = strcr & strlf;
	var strIndent = "";
	if (ArrayLen(Arguments) GT 1) {
		chri = Arguments[2];
		if (ArrayLen(Arguments) GT 2) {
			ncnt = Arguments[3];
		}
	}
	strIndent = RepeatString(chri, ncnt);
	return (strIndent & REReplace(str, "([#strcrlf#]+)([^#strcrlf#])", "\1#strIndent#\2", "ALL"));
}
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