Strings how to make them work?

Jimmy

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,021
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
 

Jimmy

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,021
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?
 

MikeHart

Administrator
Joined
Jun 19, 2017
Messages
3,210
Just calling ToChars has never worked as it is a method of a class. Which your code is missing.
 

Jimmy

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,021
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
 

Gerry Quinn

Active member
Tutorial Author
Joined
Jun 24, 2017
Messages
159
By the way, you don't even need mojo - strings are part of the language itself. Just thought I'd mention that!
 
Top Bottom