Retro Strings: Simple Blitz Basic String commands

Monkey Programming Forums/User Modules/Retro Strings: Simple Blitz Basic String commands

ImmutableOctet(SKNG)(Posted 2013) [#1]
EDIT: Any changes to this module will now be handled on github.

Current repository: GitHub

Initial release and patches for the module: Google Code (Outdated)

Since nobody seems to have made this (A module for this at least), I've done it. I pretty much just rewrote most of the old string related commands from Blitz Basic/3D/Plus. I looked at BlitzMax as a reference, and tested it, so everything should work without any problems.

This has been done for a while, I just didn't bother uploading it.

Also, I've added a couple of new functions here and there, nothing complicated, just something I threw together in five minutes.

Quite a few of these are just here for compatibility with older code. I recommend using commands like Str.Length() instead of Len(Str) with newer projects.

Command List:
* Left(Str:String, n:Int)
* Right(Str:String, n:Int)
* Mid(Str:String, Pos:Int, Size:Int=-1)
* Len(Str:String)
* Lower(Str:String)
* Upper(Str:String)
* LSet(Str:String, N:Int)
* RSet(Str:String, N:Int)
* Replace(Str:String, Sub:String, ReplaceWith:String)
* Trim(Str:String)
* Chrs(In:Int[])
* Chr(In:Int)
* Asc(Str:String)
* Ascs(Str:String)
* Hex(Value:Int)
* Bin(Value:Int)
* LongHex(Value:Int) ' This is the same as Hex for now.
* LongBin(Value:Int) ' This is the same as Bin for now.
Custom commands:
* BoolToString(In:Bool)
* ShortenedFloat(F:Float, Precision:Int=1)
* (Version 1.0.1): I forgot to update the main file to the latest version, when I uploaded this. The "Hex" command should work now.

Amon(Posted 2013) [#2]

semar(Posted 2013) [#3]
Very handy, thanks !

benmc(Posted 2013) [#4]
Awesome, thank you!

MOBii(Posted 2014) [#5]
Thank thee, very educational

ziggy(Posted 2014) [#6]
You could improve it by preventing some unneded calculations by using negative indexes on string slicing. As stated on the Monkey documentation, a negative index on a slice can be used to read from the right.
format_code('Function Right:String(Str:String, n:Int)
Return Str[Len(Str)-n..]
could be:
Function Right:String(Str:String, n:Int)
Return Str[-n ..]

ImmutableOctet(SKNG)(Posted 2014) [#7]
Thanks, Ziggy. That line has been corrected. Though, you can't really blame me too much, I made this back when I was still learning about Monkey's string-splicing functionality. Either way, the github version has been updated.

ziggy(Posted 2014) [#8]
That's nice. Also, you're creating subarrays and this adds garbage to the GC.

format_code('Function Asc:Int(Str:String)
Return Str.ToChars()[0]
Is creating an integer array to return the first value of it, However, this, avoids the array creation:

format_code('Function Asc:Int(Str:String)
Return Str[0]

Also, you're not checking for negative values on parameters, and this could make Left to return a right-like value, etc. If you want it to behave like classical basic, it should have something like:
format_code('If parameter<0 Error "Illegal function call"')

I think there are also some other bits I think could be improved, will get back to it later if you find this suggestions useful.
Thanks for the module!