Jump to content

AutoIt split string into blocks

Recommended Posts


Hello. I've got a short question for you.Ho can i split a string into blocks,with (n) characters each one?

Thanks in advance

Edited by Renderer

Share this post

Link to post
Share on other sites

Here are two different ways. One using a loop and string mid, the other using a Regexp. Both accomplish the same thing but I know a lot of people don't use/understand reg expressions

#include <Array.au3>

_ArrayDisplay(StringSplitBlocks("HelloWorld", 5), "StringSplitBlocks")
_ArrayDisplay(StringSplitBlocks2("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4), "StringSplitBlocks2")
_ArrayDisplay(StringSplitBlocks2(1234567890, 5), "StringSplitBlocks2")

Func StringSplitBlocks(Const $sString, Const $iBlockLength)
    Local $iStringLength = StringLen($sString)
    Local $aReturn[Ceiling($iStringLength / $iBlockLength)]
    Local $iCurIndex = 0

    For $i = 1 To $iStringLength Step $iBlockLength
        $aReturn[$iCurIndex] = StringMid($sString, $i, $iBlockLength)
        $iCurIndex += 1
    Return $aReturn
EndFunc   ;==>StringSplitBlocks

Func StringSplitBlocks2(Const $sString, Const $iBlockLength)
    Return StringRegExp($sString, ".{1," & $iBlockLength & "}", 3)
EndFunc   ;==>StringSplitBlocks2


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