Jump to content

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


Go to solution Solved by Malkey,

Recommended Posts

Posted

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?

Posted

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
Posted (edited)

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
Posted (edited)

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...