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

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