sandman Posted June 8, 2007 Share Posted June 8, 2007 I am trying to write a script that will split a string by its length into the usual array, to be specific a 8-character string. For example:$test = "this is a test string.... blahhhhhhh..." $split = TheFunctionIAmLookingFor($test)In the example, $split[0] would be the number of split strings, as StringSplit normally does, and$split[1] = "this is "$split[2] = "a test s"$split[3] = "tring..."$split[4] = ". blahhh"and $split[5] = "hhhh..."How could I do this?Sorry if this is confusing, my questions usually are,-- SANDMAN! [center]"Yes, [our app] runs on Windows as well as Linux, but if you had a Picasso painting, would you put it in the bathroom?" -BitchX.com (IRC client)"I would change the world, but they won't give me the source code." -Unknownsite . blog . portfolio . claimidcode.is.poetry();[/center] Link to comment Share on other sites More sharing options...
Paulie Posted June 9, 2007 Share Posted June 9, 2007 (edited) #include <array2.au3> $res=_SplitByChar("1234567890",2) _ArrayDisplay($res) Func _SplitByChar($String, $Chrs) $len = StringLen($string) $Parts = Round($len/$chrs,0)+1 $split1 = StringSplit($String, "") Local $Split2[$parts] = [""], $i = 0 For $a = 1 to $Parts-1 For $b = 1 to $chrs $Split2[$a] &= $Split1[$b+($i*$chrs)] Next $i +=1 Next $Split2[0] = $Parts-1 Return $Split2 EndFunc Edited June 9, 2007 by Paulie VetlteV 1 Link to comment Share on other sites More sharing options...
sandman Posted June 9, 2007 Author Share Posted June 9, 2007 (edited) <-- Bertha <-- says, <-- "Thank <-- you!" #include <array.au3> $res=_SplitByChar("1234567890",2) _ArrayDisplay($res) Func _SplitByChar($String, $Chrs) $len = StringLen($string) $Parts = Round($len/$chrs,0)+1 $split1 = StringSplit($String, "") Local $Split2[$parts] = [""], $i = 0 For $a = 1 to $Parts-1 For $b = 1 to $chrs $Split2[$a] &= $Split1[$b+($i*$chrs)] Next $i +=1 Next Return $Split2 EndFuncThank you!! Edited June 9, 2007 by sandman [center]"Yes, [our app] runs on Windows as well as Linux, but if you had a Picasso painting, would you put it in the bathroom?" -BitchX.com (IRC client)"I would change the world, but they won't give me the source code." -Unknownsite . blog . portfolio . claimidcode.is.poetry();[/center] Link to comment Share on other sites More sharing options...
Paulie Posted June 9, 2007 Share Posted June 9, 2007 <-- Bertha<-- says,<-- "Thank<-- you!"Thank you!!Unfortunately, it only works if the string is divided evenly by the number of characters you want to divide by. Link to comment Share on other sites More sharing options...
herewasplato Posted June 9, 2007 Share Posted June 9, 2007 (edited) Unfortunately, it only works if the string is divided evenly by the number of characters you want to divide by. try:$test = "this is a test string.... blahhhhhhh..." Dim $split[2] $i = 1 Do $split[$i] = StringLeft($test, 8) $test = StringTrimLeft($test, 8) $i = $i + 1 ReDim $split[$i + 1] Until StringLen($test) < 8 $split[$i] = $test $split[0] = UBound($split) - 1 For $i = 0 To $split[0] ConsoleWrite("$split[" & $i & "] = " & _ $split[$i] & " # of char " & _ StringLen($split[$i]) & @CR) NextYou can make it into a function if you wish. :-) Edit: added code to display array... Edited June 9, 2007 by herewasplato [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
Siao Posted June 9, 2007 Share Posted June 9, 2007 (edited) #include <Array.au3> _ArrayDisplay(_StringChop("this is a test string.... blahhhhhhh...",8)) Func _StringChop($string, $size) $count = Ceiling(StringLen($string)/$size) Dim $array[$count+1], $start = 1 For $i = 1 To $count $array[$i] = StringMid($string, $start, $size) $start += $size Next $array[0] = $count Return $array EndFunc Edited June 9, 2007 by Siao "be smart, drink your wine" Link to comment Share on other sites More sharing options...
herewasplato Posted June 9, 2007 Share Posted June 9, 2007 Siao, add $array[0] = UBound($array) - 1 above _ArrayDisplay($array) to make the OP happy :-) "...$split[0] would be the number of split strings..." Seriously, nice solution - I started with StringMid and changed to StringLeft and StringTrimLeft for some silly reason. StringMid is probably the better method. -MSP- [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
Siao Posted June 9, 2007 Share Posted June 9, 2007 (edited) Whoops, forgot about that... done. Edited June 9, 2007 by Siao "be smart, drink your wine" Link to comment Share on other sites More sharing options...
herewasplato Posted June 9, 2007 Share Posted June 9, 2007 Whoops, forgot about that...done.Oh sure - do it the easy way with your $count var from the ceiling math...Man, this is not my night for reading or writing code :-(Don't be a stranger...-MSP- [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
thor918 Posted June 17, 2007 Share Posted June 17, 2007 thanks for the code. I used it to split hex from regread command Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now