CFLib.org – Common Function Library Project

stringToArray(string, count)

Last updated June 12, 2003

author

Aidan Whitehall

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Accepts a string and a numeric value, chops the string into x character lengths and returns them in an array.

Return Values:
Returns an array.

Example:

<cfscript>
// Output a continuous CC number with dashes.
myCC = stringToArray("1234123412341234", 4);

for (i = 1; i lte arrayLen(myCC); i = i + 1) {
   writeOutput(myCC[i] & iif(i eq arrayLen(myCC), de(""), de("-"))); }

</cfscript>

Parameters:

Name Description Required
string String to parse. Yes
count Number of characters per array index. Yes

Full UDF Source:

/**
 * Turns a string to an array of 'count' character chunks.
 * 
 * @param string 	 String to parse. (Required)
 * @param count 	 Number of characters per array index. (Required)
 * @return Returns an array. 
 * @author Aidan Whitehall (aidan@thenetprofits.co.uk) 
 * @version 1, June 12, 2003 
 */
function stringToArray(string, count) {
   var array = arrayNew(1);
   
   while (len(string)) {
      arrayAppend(array, left(string, min(count, len(string))));
      
      if ((len(string) / count) gt 1) string = right(string, len(string) - count);
      else string = "";
   }

   return array;
}
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