Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


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

    I'm old - what's your excuse?

  • Moderators
  • 22,104 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
Date_Time_Convert - Easily convert date/time formats, including the language               GUIListViewEx - Insert, delete, move, drag, sort and edit ListView items

#3 somdcomputerguy

somdcomputerguy

  • Active Members
  • PipPipPipPipPipPip
  • 2,869 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

  • MVPs
  • 7,184 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
  • 5,373 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 applies to SQLite as well)

An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.

 

SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.

 

PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

RegExp tutorial: enough to get started

Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users