Sign in to follow this  
Followers 0
erifash

StringChunk

7 posts in this topic

#1 ·  Posted (edited)

I have created the AutoIt version of PHP's stringchunk function. The function divides the string into equal pieces (with the last piece being anything left over) using a separation character (default is @CRLF). Hope you like it! ;)

Func _StringChunk( $str, $chrs, $sep = @CRLF )
    Local $len = StringLen($str), $ret = ""
    For $i = 1 to $len step $chrs
        $ret &= StringMid($str, $i, $chrs) & $sep
    Next
    Return StringTrimRight($ret, StringLen($sep))
EndFunc

Questions/comments/suggestions are greatly appreciated!

EDIT: Speed improvement.

Edited by erifash

Share this post


Link to post
Share on other sites



I'm slightly confused by your description -- does it find the length of the string before the first delimiter and then break up subsequent pieces of the string with the length of the first piece?

Share this post


Link to post
Share on other sites

I'm slightly confused by your description -- does it find the length of the string before the first delimiter and then break up subsequent pieces of the string with the length of the first piece?

<{POST_SNAPBACK}>

It doesn't find delimeters, it uses character indexes. Here's an example to help clarify:

$str = "abcdefghijklmnopqrstuvwxyz"
$chunk = 3
MsgBox(0, "regular string", $str)
$chunky = _StringChunk($str, $chunk)
MsgBox(0, "chunky string", $chunky)

I hope that helped.

Share this post


Link to post
Share on other sites

Ooh, I understand now and this is quite handy. In fact I know just the thread to report this.

Thanks for the clarification.

Share this post


Link to post
Share on other sites

Ooh, I understand now and this is quite handy. In fact I know just the thread to report this.

Thanks for the clarification.

<{POST_SNAPBACK}>

Thanks for the compliment. By the way, what thread are you going to report this?

Share this post


Link to post
Share on other sites

String Insert -- I'm Brain Fried :(

I'm trying to insert a hyphen in my string every 3rd letter or number... but every attempt I've tried for the past couple of hours has failed.

<{POST_SNAPBACK}>

Share this post


Link to post
Share on other sites

Sorry to resurrect an old topic but I find this function very useful at times. Also, I have optimized the code for a significant speed improvement so it is worth checking out! ;)

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