common string functions in BMAX?
BlitzMax Forums/BlitzMax Beginners Area/common string functions in BMAX?
| ||
I'm very experienced in programming, but brand new to BMax. I see that there are a bunch of string methods now, since strings are now technically objects. This is great, but I don't see a few basic methods like Left, Right, Mid. Is it correct to use these functions in the old-school format, or are they only there for backward compatibility with old BASIC code? It seems like they would be methods of the string type. |
| ||
There's a BRL.Retro module which has those functions you are missing. They are 1 indexed. The built-in methods (of String) use what's known as slicing. These are zero indexed. For example : Local s:String = "1234567890" Print Left(s, 4) Print s[..4] Print Mid(s, 4, 3) Print s[3..6] Print Right(s, 4) Print s[s.length - 4..] See the (very short) guide on slicing in the IDE, if you want more information on that. Slicing is a bit more efficient, since you are skipping a function call/return. |
| ||
Thanks Brucey. I actually noticed this stuff right after I posted that, but I could hardly believe the strangeness of it (especially the "Right" one). I mean, why not create string methods by these names that do the basic math for you and return the slices. Oh well. Is there a way to add methods to an existing type? I'll do it myself if I can. The way slices is implemented, it's not consistent. [X..] means get the values starting with index X [X..Y] means get values of index X-Y [..X] means get the first X values (instead of values UP TO index X) |
| ||
I believe if you want to add methods the best way is to extend the brl type and add new methods, eg:Type My_TSound Extends TSound Method NewMethod() end method End Type |
| ||
String is not a "real" type, per se, and therefore you can't extend it. Think of it more as a primitive type than a... type type... :-p |
| ||
[X..] means skip the first X elements and return the rest [..X] means return the first X elements and skip the rest [X..Y] means return the first Y elements after skipping the first X elements One way to think about it that seems more consistent. |