Renderer Posted December 3, 2016 Share Posted December 3, 2016 (edited) 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 December 3, 2016 by Renderer Link to comment Share on other sites More sharing options...
InunoTaishou Posted December 3, 2016 Share Posted December 3, 2016 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now