• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Generating a string of arbitrary length

olive

Member
3rd Party Module Dev
Tutorial Author
Patreon Bronze
Joined
Jul 17, 2017
Messages
76
I've run into a case where I need to generate a string of X length and was surprised that I didn't actually know a quick way to do it in CX. Only way I know how would be with a loop:

Code:
Local string$
For Local i% = 0 Until 32
  string = string + "A"
Next

Anyone know of a more elegant/memory-efficient way? E.g. in Ruby you can just go "A" * 32
 
Not sure if it is more elegant or memory efficient but this one:


Code:
Local s:String = String.FromChars( [65,66,67,32,48,60] )
 
Behind the scenes, Ruby will covert the "A"*32 into a loop to generate a string of characters.
The question to be asking, would it be faster to deal directly with strings or deal directly with an array to convert to a string after.
All this depends on how efficient Mr S made the native core string and array handling classes.
 
You could do this, just have some string laying around that is long enough :

Code:
Strict

Function Main:Int()

     Local a:String = "0123456789abcdefghijklmnopqrstuvwxyz"
    Local b:String = ""

      ' Create string b with some length
     Local IWantLength:Int = 10
     b = Mid(a,1,IWantLength)

' This would do the same :
' b = a[(0)..(IWantLength)]

     Print b

    Return 0        
End


Function Len:Int(s:String)
    Return s.Length
End

Function Mid:String(s:String,p:Int,n:Int)
    p=p-1
    Return s[(p)..(p+n)]
End
 
Last edited:
Ha! That's very creative, I like it. Yeah, there's a max length that I might ask for in my case (showing asterixes in place of characters in a password text field), so I could just pre-make a long string of them and slice it. Thanks!
 
FromChars is the way to go. If you know the length in advance you can use an array, if not you can use a Stack to get a sort of StringBuffer.
 
Back
Top Bottom