• 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

Strings how to make them work?

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,414
I had a strange issue getting strings to work, I'm importing mojo2 should that not be enough?
Getting crazy finding what is wrong?
String.png


Code:
Import mojo2

Function Main()
    New Game
    Return 0
End

Class Game Extends App

    Method OnCreate()

           Print  ToChars("test")[0]
    
      ' Print Chr(50)
 
        Return 0
    End

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

Function Left:String(s:String,n:Int)
    Return s[0..n]
End

Function Right:String(s:String,n:Int)
    Return s[(s.Length()-n)..(s.Length())]
End

Function Chr:String(n:Int)
    Return FromChar(n)
End

Function Val:Int[](s:String)
    Return ToChars(s)[0]
End
 
Btw I know the functions worked at some point (2019-2021) as I wrote and tested them around that time, so I knoe they worked once.

EDIT
Now I have a new CX version (tried on macOS, Cerberus V2021-07-25, and Linux 2021-10-15), same problem.
What am I doing wrong?
 
Just calling ToChars has never worked as it is a method of a class. Which your code is missing.
 
Ops correct that. Ya that was terrible. Thanks anyways!

Code:
Import mojo2

Function Main()
    New Game
    Return 0
End

Class Game Extends App

    Field strs$[8]
    Field ascii$

    Method OnCreate()

        Print Len("test")
        Print Mid("test",2,2)
        Print Left("test",2)
        Print Right("test",2)
        Print Chr(65)
        Print Val("A")

        Return 0
    End
  
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

Function Left:String(s:String,n:Int)
    Return s[0..n]
End

Function Right:String(s:String,n:Int)
    Return s[(s.Length()-n)..(s.Length())]
End

Function Chr:String(n:Int)
    Local result:String
    result = result.FromChar(n)
    Return result
End

Function Val:Int(s:String)   
        Local chrs:Int[] = s.ToChars()
        Return chrs[0]
End
 
By the way, you don't even need mojo - strings are part of the language itself. Just thought I'd mention that!
 
Back
Top Bottom