Jump to content



Photo

Split array into string of fixed number of characters


  • Please log in to reply
5 replies to this topic

#1 kritya

kritya

    Seeker

  • Active Members
  • 10 posts

Posted 28 April 2012 - 02:40 PM

Hi,

I have string , which I want to split into strings of equal size(characters) lets say 4 or 5.


Did some searching. Found this

$aNumber = StringRegExp($string, "\d{4}", 3)


This only works on numbers and not on general string.

Please Help me.

Thanks





#2 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,358 posts

Posted 28 April 2012 - 02:53 PM

kritya,

Just look for any character and not just digits: :)
#include <Array.au3> $sString = "1234abcd5678ghij1234abcd5678ghij" $aArray = StringRegExp($sString, ".{4}", 3) _ArrayDisplay($aArray)

If you want to start learning more about SREs, then I recommend this site. ;)

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#3 somdcomputerguy

somdcomputerguy

  • Active Members
  • PipPipPipPipPipPip
  • 2,380 posts

Posted 28 April 2012 - 02:54 PM

Take a look at StringLen, and it's related String functions.
- Bruce /* somdcomputerguy */If you change the way you look at things, the things you look at change.

#4 czardas

czardas

  • Active Members
  • PipPipPipPipPipPip
  • 5,065 posts

Posted 28 April 2012 - 03:21 PM

#include <Array.au3> Dim $aArray = _StringEqualSplit("abcdefghijklmnopqrstuvwxyz", 5) _ArrayDisplay($aArray) Func _StringEqualSplit($sString, $iNumChars)     Return StringRegExp($sString, "(?s).{1," & $iNumChars & "}", 3) EndFunc


Hmm I didn't read Melba's responce. It's very nearly the same - the only difference is that any odd number of remaining characters are added to the final element.

Edited by czardas, 28 April 2012 - 03:27 PM.


#5 kritya

kritya

    Seeker

  • Active Members
  • 10 posts

Posted 28 April 2012 - 03:25 PM

kritya,

Just look for any character and not just digits: :)

#include <Array.au3> $sString = "1234abcd5678ghij1234abcd5678ghij" $aArray = StringRegExp($sString, ".{4}", 3) _ArrayDisplay($aArray)
If you want to start learning more about SREs, then I recommend this site. ;)

M23

Yes , I know that.

I am not good with regex but yea I can search it Posted Image but wasn't able to find on this Posted Image

And well by using I made that.
Thanks all.
Code :

#include <Array.au3> $string="1 2 3 4 5 6 7 8 9 1011" $len=StringLen($string) MsgBox(0,"",$len) $len=$len/4 Local $arr[$len+1] MsgBox(0,"",$len) For $i=0 To $len    $arr[$i]=StringMid($string,$i*4+1,4) Next  _ArrayDisplay($arr)

Any Better way of doing this or any kind of problem in this that may be faced in BIG strings or is the best method ?

Thanks

Edited by kritya, 28 April 2012 - 03:33 PM.


#6 jchd

jchd

    Whatever your capacity, resistance is futile.

  • MVPs
  • 3,251 posts

Posted 28 April 2012 - 10:09 PM

The regexp should run rings around your loop (on large strings).
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQL tutorial (covers generic SQL, but most of it apply to SQLite as well)An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious workPCRE v8.32 regexp pattern documentation. AutoIt uses a slightly older version so that more advanced features are not all available.RegExp tutorial: enough to get started




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users