#138598 - 2005-04-25 05:12 AM
Segment a string into variable length segments
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
I am trying to take a string that may be 200-500 characters long and break it into segments that are no more that 80 characters long.
however, the point of segmenting must be on a comma, so it will have to be smart enough to find the closest comma <80 characters in the string.
I will also be concatenating the string with '\'+@crlf+' '
basically, I am reading the reg of a REG_EXPAND_SZ type value and converting it into a format identical to regedit export.
Code:
%USERPROFILE%\Local Settings\Temp
into
"TMP"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,4c,00,\ 45,00,25,00,5c,00,4c,00,6f,00,63,00,61,00,6c,00,20,00,53,00,65,00,74,00,74,\ 00,69,00,6e,00,67,00,73,00,5c,00,54,00,65,00,6d,00,70,00,00,00
I already have bryce's Function RegASCIItoHEX() to convert the readvalue() into the hex string, I need to break it to add the '/' + @crlf
TIA
Edited by Radimus (2005-04-25 05:15 AM)
|
|
Top
|
|
|
|
#138599 - 2005-04-25 10:20 AM
Re: Segment a string into variable length segments
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
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=",")
|
|
Top
|
|
|
|
#138600 - 2005-04-25 12:48 PM
Re: Segment a string into variable length segments
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
|
|
Top
|
|
|
|
#138601 - 2005-04-25 02:27 PM
Re: Segment a string into variable length segments
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Oops!
Cut'n'paste typo in the bit of code handling the remaining string.
Code should be: 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)]=$s EndIf Exit 0 EndFunction
|
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 1257 anonymous users online.
|
|
|