StringRepeat(text, num)

Post your working scripts, libraries and tools for AHK v1.1 and older
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

StringRepeat(text, num)

05 Oct 2016, 09:09

Greetings,

Here's a basic working StringRepeat function I wrote because I couldn't find one in the docs.
Is there a more ahkonic way of doing this?

Code: Select all

; ============== StringRepeat ==============
; returns text concatenated to itself n times 
StringRepeat(text, num) {
    repeated = 
    Loop, %num% {
       repeated := repeated . text
       }
    Return repeated
}
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: StringRepeat(text, num)

05 Oct 2016, 09:43

I don't know if you or anyone else would consider the following more "ahkonic":
You can shorten the code a bit by using the build-in ahk way of initializing local variables.
You can shorten the code a bit by using the .= operator.
You can shorten the code a bit by omitting the unnecessary braces.

Code: Select all

MsgBox, % StringRepeat("Text", 3)

StringRepeat(Text, Count) {
    Loop, %Count%
       Result .= Text
    Return, Result
}
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Re: StringRepeat(text, num)

05 Oct 2016, 10:39

Nice! Thank you for these improvements, @wolf_II

I had tried the compound concatenation operator without success and concluded it wasn't available --- I must have had another bug at the time.

The inline initialization is new to me. And the lack of braces blows my mind -- used to indentation-based control in Python but wasn't expecting it here.

And how did you mark up the operator to make it look so cool?
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: StringRepeat(text, num)

05 Oct 2016, 10:59

I don't know if those qualify as "improvements;" maybe just changes in style which is just personal preference.

It's not "indentation-based control."
Blocks wrote:If an IF, ELSE, Loop, While-loop, or For-loop has only a single command, that command need not be enclosed in a block. However, there may be cases where doing so enhances the readability or maintainability of the script.
If you omit the braces, only one line is used as the body of the loop.

.= - You can use the [ c ]... [ /c ] tags for inline code. Click the "Full Editor & Preview" button when posting a comment to see more of the tags that are available. You can also click the quote icon (") on the top-right of a post and it will show you the BBCode tags that were used (you don't have to actually post the quoted reply).
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Re: StringRepeat(text, num)

05 Oct 2016, 11:08

@kon
Thank you. Super helpful tips. I've learned loads today.
Hitting the quote button should work on any forum, never thought of that, adding that to my bag of tricks.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: StringRepeat(text, num)

05 Oct 2016, 12:17

For many string concatenations, you might want to use varsetcapacity.
Hint: you don't need to do num concatenations for Num>2.
Johnny R
Posts: 348
Joined: 03 Oct 2013, 02:07

Re: StringRepeat(text, num)

06 Oct 2016, 01:35

For many string concatenations, you might want to use varsetcapacity.
Look here by Skan:

Code: Select all

Replicate( Str, Count ) { ; By SKAN / CD:12-04-2011
; www.autohotkey.com/community/viewtopic.php?p=435990#435990
 VarSetCapacity( S, Count * ( A_IsUnicode ? 2:1 ), 1 )
 StringReplace, S, S, % SubStr( S,1,1 ), %Str%, All
Return SubStr( S, 1, Count * StrLen(Str) )
}

; Usage Examples

MsgBox, % "[" Replicate( A_Space, 100 ) "]"
MsgBox, % Replicate( "B@BY" , 100 )
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Re: StringRepeat(text, num)

06 Oct 2016, 02:36

@Johnny R
Thank you for that piece of deep wizardry!

Hard on the eyes, but more efficient as we the variable only gets allocated once.
I hesitate to integrate this kind of code as I wouldn't feel comfortable maintaining it, but would gladly use a built-in StrRepeat() function that avoids serial reallocation.

Do you happen to know if a string builder construct is in the works for upcoming releases?
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: StringRepeat(text, num)

06 Oct 2016, 03:14

I guess the function strreplace wasn't available when that function was written, it would probably be more efficient to use that, and specify Count instead of all (-1). I didn't try though.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 59 guests