Things with Strings
Submitted:|| 15th August, 2002
Yeah, it's me again writing long articles about stuff. This one is thankfully only in one part and is about sub-strings.
What are they?
There's a nifty feature in MMF called getting sub-strings. The options can be found under 'Strings' in the Special Object. If you have TGF you can get these with String Parser. Anyway, with these you can get the Left$, Right$, Mid$ and Len of strings. If you don't understand that, then:
Left$("String",4) returns the 4 leftmost characters of "String", in this case "Stri".
Right$("Example",5) returns the 5 rightmost characters of "Example", in this case "ample".
Mid$("I eat food,2,6) starts 2 characters in, and returns the next 6 characters, in this case "eat fo".
Len("This is a string") returns the length of a string, in this case 16.
However, there is more to them than this. There's loads of stuff to do with them by adding a couple loops and an edit box.
Some simple expressions
They look complicated, but most are actually very simple. Like this one:
Left$( "One less", Len( "One less" ) - 1 )
This returns One less with it's end chopped off - ie, "One les". You don't have to use a string in double quotes, you can give it String$( "String object" ) or EditText("Edit object") if you wanted to.
It works by getting the left substring of "One less" and one less than how long it is. If it was 8 letters long, it would return the first 7 letters, which is what ot does here. If it was - 2 instead of 1, it would return "One le". Likewise, if it was Right$ instead of Left$ it would return ne less - the last 7 letters.
On the topic of removing characters:
Right$( "Backflip", 1 ) + Left$( "Backflip", Len( "Backflip" ) - 1 )
This is the last letter of "Backflip" (p) added to the first 7 letters (Backfli) to make "pBackfli". It uses the last letter removal technique above to get the first 7 letters. However, be careful when modifying this: You have to change both 1s or you may end up with "pBackfl" or something. Also, more importantly, the opposite of this is:
Right$( "Backflip", Len( "Backflip" ) - 1 ) + Left$( "Backflip", 1 )
As well as changing the Left$s and Right$s around, you have to swap the expressions on either side of the + around. Otherwise it would be "B" + "ackflip" which does nothing interesting.
You can cut off letters at each end with:
Mid$( "Cuttin' Down", 1, Len( "Cuttin' Down" ) - 2 )
Which returns "uttin' Dow." This begins 1 letter in, and continues for the length - 2 letters (which is 10 letters). You should be able to work out what most of these mean by now. If so, try:
Left$( "The Final Test", Len( "The Final Test" ) / 2 - 1 ) + Right$( "The Final Test", Len( "The Final Test" ) / 2 - 1 )
Yes, it's long, and yes, it's got some division in it. Try testing it and see what it does, then try to think how it works.
Using the Fastloop
MMF has it's own Fastloop now, however, I prefer using the Cellosoft one because it's faster. Ish. I think. Anyway. For these you need the Fastloop object and two string objects. The strings we use change, so we have to store them somewhere.
Set String 1 to "salsa sauces"
Start a loop for Len(string$( "String 1" )) times
AND Left$( string$( "String 1" ), 1 ):
Set String 2 to string$( "String 2" ) + Left$( string$( "String 1" ), 1 )
Set String 1 to Right$( string$( "String 1" ), Len( string$( "String 1" ) ) - 1 )
The output of this should be ala auce. Yep, it's removed all the s letters from the string. Basically, what it does is check each letter, and if it isn't an s put it into another string. Then it changes the strings to make the next letter checked.
This is very versatile. You could easily make it so if it IS an s that is being checked, change it to an n. Or add another condition to the second event to change n and s around.
That's all from me. I may write another article when I think up some more of these. By the way, in the River section of the Mac game System's Twilight there was a puzzle where you had to manipulate words to get other words. I've yet to see a Click game based on this...