Jump to content

AutoIt split string into blocks


 Share

Recommended Posts

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
    Next
    Return $aReturn
EndFunc   ;==>StringSplitBlocks

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

 

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...