Sign in to follow this  
Followers 0

Split array into string of fixed number of characters

6 posts in this topic

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



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

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

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

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

Posted

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

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




  • Recently Browsing   0 members

    No registered users viewing this page.