Jump to content
Sign in to follow this  
maniootek

How to cut my string into pieces and save each piece to new element of array

Recommended Posts

maniootek

I want to cut my string into 8 digit long pieces

$string = "42383298472398472389472398472389"

and place each result into array
 

$aResult = StringCut($string, 8)

any idea?

Share this post


Link to post
Share on other sites
Malkey

Here is one method.

#include <Array.au3>

$string = "42383298472398472389472398472389"
$array = StringRegExp($string, "\d{8}", 3) ; From left to right, match 8 digits continuously.
_ArrayDisplay($array)

Share this post


Link to post
Share on other sites
gil900

If you want to learn how it can be done without StringRegExp() then:

#include <Array.au3>

$string = "42383298472398472389472398472389"
$test = StringCut($string,8)
_ArrayDisplay($test," (Line: "&@ScriptLineNumber&")")

Func StringCut($string,$numb)
    Local $tmp1,$tmp2[1]
    $tmp2[0] = 0
    $tmp1 = $string
    Do
        _ArrayAdd($tmp2,StringLeft($tmp1,$numb))
        $tmp2[0] += 1
        $tmp1 = StringTrimLeft($tmp1,$numb)
    Until $tmp1 = ''
    Return $tmp2
EndFunc

Share this post


Link to post
Share on other sites
andrewz

U could also do it this way:

Count lenght --> divide it by 8 = x --> StringTrimRight by 8 x times and StringTrimLeft by times ran--> substract 1 from x -> Save into Array --> Do untill x = 0.

There are so many possible ways, tho Malkey's solution looks like the "cleanest" and maybe is even the best resource wise.

EDIT: Here u go, just with a massagebox tho... :

#include <Array.au3>

$string = "42383298472398472389472398472389"
$lenght = StringLen($string)
$timestorun = $lenght/8
$timesran = 0

Do
    $timestorun -= 1
    $cut_A = $timestorun *8
    $cut_B = $timesran *8
    $string_A = StringTrimRight($string,$cut_A)
    $string_B = StringTrimLeft($string_A,$cut_B)
    $timesran += 1
    MsgBox(0,"",$string_B)

    Until $timestorun = 0
Edited by andrewz

Share this post


Link to post
Share on other sites
maniootek

Thank you guys 

Share this post


Link to post
Share on other sites
kylomas

This handles strings if not a multiple of 8...

#include <Array.au3>

$string = "4238329847239847238947239847239876"
$array = StringRegExp($string, "\d{1,8}", 3) ; From left to right, match 8 digits continuously.
_ArrayDisplay($array)

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×