It's not as tricky as it sounds, thanks to InStrRev().
This UDF will return a array of the string broken at the delimiter before the required length.
Both delimiter and length are option and default to "," and 80 respectively.
If you pass a blank string you will get an array back with a single blank element.
If there is not a delimiter within the specified length then the string is split at the first available delimiter.
Code:
Function ChopString($s,Optional $l, Optional $d)
Dim $i
; Handle optional parameters
$l=CInt($l)
If Not $l $l=80 EndIf
If $d="" $d="," EndIf
; Deal with null strings
If $s="" Redim Preserve $ChopString[0] Exit 0 EndIf
; Break out long string lengths
While Len($s)>$l
$i=InStrRev(Left($s,$l),$d)
If $i<0 $i=InStr($s,$d) EndIf
Redim Preserve $ChopString[UBound($ChopString)+1]
$ChopString[UBound($ChopString)]=Left($s,$i)
$s=SubStr($s,$i+1)
Loop
; Grab what's left of the string
If $s<>""
Redim Preserve $ChopString[UBound($ChopString)+1]
$ChopString[UBound($ChopString)]=Left($s,$i)
EndIf
Exit 0
EndFunction
Parameters:
- $s = String to split
- $l = Maximum length of parts (Optional, default=80)
- $d = Delimiter to break on (Optional, default=",")