Sign in to follow this  
Followers 0
FinalVersion

Grab every 4 characters of a string

9 posts in this topic




#include <Array.au3>
$string = "0000100001000010000100001000010000100001"

$aString = _GetEvery ($string, 5)
MsgBox (0, "", $aString)
_ArrayDisplay ($aString)

Func _GetEvery ($string, $iTImes)
    Local $count = 0
    $len = StringLen ($string)
    Local $aStrings[Int($len/$iTImes)+1]
    For $i = 1 To $len + 1 Step $iTImes
        $aStrings[$count] = StringMid ($string, $i-1, 1)
        $count += 1
    Next
    Return $aStrings
EndFunc

Share this post


Link to post
Share on other sites

Or with the power of RegExp:

#include <Array.au3>
$string = "0000100001000010000100001000010000100001"

$aString = _GetEvery ($string, 5)
_ArrayDisplay($aString)

Func _GetEvery ($string, $iTimes)
    $aString = StringRegExp($string, ".{" & $iTimes-1 & "}(.)",3)
    Return $aString
EndFunc

Share this post


Link to post
Share on other sites

Or with the power of RegExp:

#include <Array.au3>
$string = "0000100001000010000100001000010000100001"

$aString = _GetEvery ($string, 5)
_ArrayDisplay($aString)

Func _GetEvery ($string, $iTimes)
    $aString = StringRegExp($string, ".{" & $iTimes-1 & "}(.)",3)
    Return $aString
EndFunc

Hehehehe was trying something like that but I just gave up and went old school... :mellow:

Still learning those damn things 8)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Actually neither of your solutions does what he wants -- as I understand it. He wants to get every x characters into an array. So if you have the string "12345678" he want to have the result $a[0] = "1234", $a[1] = "5678".

Modified the code of a ResNullius a bit to provide this. This ofcourse only works properly when StringLen($string) mod $iTimes = 0. If not, the last characters < $iTimes won't appear in the array.

#include <Array.au3>
$string = "0000100001000010000100001000010000100001"

$aString = _GetEvery ($string, 5)
_ArrayDisplay($aString)

Func _GetEvery ($string, $iTimes)
    $aString = StringRegExp($string, "(.{" & $iTimes & "})",3)
    Return $aString
EndFunc
Edited by d4ni

Share this post


Link to post
Share on other sites

Modified the code of a ResNullius a bit to provide this. This ofcourse only works properly when StringLen($string) mod $iTimes = 0. If not, the last characters < $iTimes won't appear in the array.

When last characters are required
#include <Array.au3>
$string = "0000100001000010000100001000010000100001001"

$aString = _GetEvery ($string, 5)
_ArrayDisplay($aString)

Func _GetEvery ($string, $iTimes)
    $aString = StringRegExp($string, "(.{" & $iTimes & "}|.{1," & $iTimes & "})",3)
    Return $aString
EndFunc

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

No need for the OR operation, you can just do it like this:

#include <Array.au3>
$string = "0000100001000010000100001000010000100001"

$aString = _GetEvery ($string, 5)
_ArrayDisplay($aString)

Func _GetEvery ($string, $iTimes)
    $aString = StringRegExp($string, "(.{1," & $iTimes & "})",3)
    Return $aString
EndFunc

It's greedy by default so always tries to match $iTimes characters first, when it fails steps back to $iTimes-1, etc.

I just figured he might have wanted it to not include characters that are not a multiple of x :mellow:

Edited by d4ni

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

@d4ni

Exactly what I wanted, I just had to change $iTimes to 4.

Credits go to ResNullius who started the Regular Expression. Picaxe and I only modified it a bit.

Remember you don't have to change $iTimes directly, but only the number you use in the function call _GetEvery -- it's 5 there now, so if you just make that 4 it'll work.

Edited by d4ni

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  
Followers 0