Retro Strings: Simple Blitz Basic String commandsMonkey Programming Forums/User Modules/Retro Strings: Simple Blitz Basic String commands
| 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.
* Left(Str:String, n:Int)
* Right(Str:String, n:Int)
* Mid(Str:String, Pos:Int, Size:Int=-1)
* LSet(Str:String, N:Int)
* RSet(Str:String, N:Int)
* Replace(Str:String, Sub:String, ReplaceWith:String)
* LongHex(Value:Int) ' This is the same as Hex for now.
* LongBin(Value:Int) ' This is the same as Bin for now.
* 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.
| Cool! |
| Very handy, thanks ! |
| Awesome, thank you! |
| Thank thee, very educational |
| 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)
Function Right:String(Str:String, n:Int)
Return Str[-n ..]
| 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. |
| That's nice. Also, you're creating subarrays and this adds garbage to the GC.|
Is creating an integer array to return the first value of it, However, this, avoids the array creation:
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!