to parse a long string into an array of sub strings of an arbitrary length.
I'm posting my solution here both to wrap up the thread incase anybody else
ever needs to do something like this, as well as for further review on how
this code may still be optimized.
Thank You
------------
--- Code ---
------------
<cffunction name="stringParse" output="false" returntype="array"
hint="Process a long string into an array of substrings of an arbitary
length.">
<cfargument name="string" required="true" type="string" displayname="String"
hint="String to be processed into the array.">
<cfargument name="chrLimit" required="true" type="numeric"
displayname="Character Limit" hint="Arbitary length of the substrings.">
<!--- Initialize Variables. --->
<cfset var pointer = 1>
<cfset var element = 1>
<cfset var subStringAry = arrayNew(1)>
<cfscript>
while (pointer LTE len(string))
{
// Find the next line feed [chr(10)LF] in the string.
LFpos = Find(chr(10),string,pointer);
// If no line feed found, set LFpos to the end of the string.
if (NOT LFpos)
{
LFpos = len(string);
}
// Set LFsubStr to the mid of the string from pointer to LFpos and
increment pointer.
LFsubStr = mid(string,pointer,LFpos - (pointer - 1));
pointer = LFpos + 1;
if (Len(LFsubStr) LTE chrlimit)
{
// substring is less then character limit then, Set the next
subString element to the LFsubStr and increment element.
subStringAry[element] = LFsubStr;
element = element + 1;
}
else
{
// Process substring over character limit into further
substrings broken at white space characters.
start = 1;
while (start LT len(LFsubStr))
{
// Find last white space character before charlimit.
sLastSpace = REFind('.{' & start & ',' & start +
chrlimit - 2 & '}(\s)', LFsubStr, 1, 1);
if (arraylen(sLastSpace.Pos) EQ 2 AND len(LFsubStr)
- start GT chrlimit)
{
// If white space is found create sub string.
subString =
mid(LFsubStr,start,sLastSpace.Pos[2]-start+1);
}
else
{
// else create new substring from rest of original
substring.
substring =
right(LFsubStr,len(LFsubStr)-start+1);
}
if (len(subString) LTE chrlimit)
{
// If new substring is less then or equal chrlimit
place into array.
subStringAry[element] = subString;
element = element + 1;
start = start + len(subString);
}
else
{
//else divide new substring into a chrlimit piece
and place into array.
subStringAry[element] =
left(subString,chrlimit);
element = element + 1;
start = start + chrlimit;
}
}
}
}
</cfscript>
<cfreturn subStringAry>
</cffunction>
--------------
Ian Skinner
Web Programmer
BloodSource
www.BloodSource.org
Sacramento, CA
Confidentiality Notice: This message including any
attachments is for the sole use of the intended
recipient(s) and may contain confidential and privileged
information. Any unauthorized review, use, disclosure or
distribution is prohibited. If you are not the
intended recipient, please contact the sender and
delete any copies of this message.
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings]

