Sign in to follow this  
Followers 0

Split array into string of fixed number of characters

6 posts in this topic

#1 ·  Posted

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

Share this post


Link to post
Share on other sites



#2 ·  Posted

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert - Easily convert date/time formats, including the language used
ExtMsgBox ------------- A highly customisable replacement for MsgBox
GUIExtender ----------- Extend and retract multiple sections within a GUI
GUIFrame -------------- Subdivide GUIs into many adjustable frames
GUIListViewEx -------- Insert, delete, move, drag, sort and edit ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ---------------- Scrolling tickertape GUIs
NoFocusLines --------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify -------------------- Small notifications on the edge of the display
Scrollbars -------------- Automatically sized scrollbars with a single command
StringSize -------------- Automatically size controls to fit text
Toast -------------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

#3 ·  Posted

Take a look at StringLen, and it's related String functions.


- Bruce [font="'Lucida Console';"]/*somdcomputerguy */  If you change the way you look at things, the things you look at change.[/font]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

#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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

StringMid()
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

Share this post


Link to post
Share on other sites

#6 ·  Posted

The regexp should run rings around your loop (on large strings).


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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
Sign in to follow this  
Followers 0