Jump to content

Grab every 4 characters of a string


Recommended Posts

#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

Link to comment
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)

Link to comment
Share on other sites

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
Link to comment
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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

@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
Link to comment
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
 Share

  • Recently Browsing   0 members

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