Range Comparisons with Strings

NOTE: Strings as used here just means 'Text'. In computers, we refer to any text as a 'String of characters'.

This article is really quite simple, but it's a feature I only recently discovered, and which yields cool results, so we're going to explore it. It'll probably turn out everyone already knew this, but you never know.

Comparisons We're Used To...
--------------------------------------------

We're all used to things like the following:

X Position of player IS GREATER THAN '200' pixels.

Alterable Value A IS LOWER OR EQUAL TO '150'.

Basic comparisons, aren't they? We use them in conditions. You've probably also seen your fair share of these in your lifetime:

Alterable String A IS EQUAL TO YES


Edittext$(PASSWORD) IS DIFFERENT TO FloppsyBunnies24

That kind of thing. It's basic.

We use 'IS EQUAL TO' and 'IS DIFFERENT TO' comparisons with strings all the time, to test is some string matches another one. But we tend not to use the other options for comparing text: Is Lower Than or Equal to, Is Lower Than, Is Greater Than, Is Greater Than or Equal to...

...Do these have any special useage with Strings?

Using Range Operators with Strings

The above examples (lower than, greater than) are called Range Operators, and the others (equal to, different to) have some name that I don't know, but I call them Match Operators.

To see how useful Range Operators are with strings, we need to know a little more about what strings are...

What Strings Are...

A String is so called because it's a string of characters, one after the other. Each character is represented by an ID Number. With a standard ASCII typset (the system used by MMF2), there are 255 possible characters.

You can see all these characters in a table at www.asciitable.com - I recommend you look at it now.
Leave it open in a separate tab or window, we'll be referring back to it.

Now, ASCII is as old as the hills, so the first few characters are almost never used. Things like the BEL (Bell) character, the EOT (End of Transmission) character, and others. These are command characters, used in the old days of electronic typewriters and the like.

Some are still used - notice TAB in there? CR (Carriage Return) and LF (Line Feed) are also still commonplace. They're what build up a standard newline. So in MMF, if you create a string, and do this:

1
2

It looks like it should have only 2 characters (1 and 2) but it actually has FOUR: 1, 2, and the CR and LF characters in between them, making up the new line.

But I digress. You should be able to see how characters like 1, 2, 3, ... 9 are all arranged in numerical order. Alphabetical characters also come in order. The CAPITALISED versions come earlier than the lower case versions.

And of course, there are special characters like +, -, =, [, ], {, } and a bunch of others crammed in their somewhere too.

Each character has an ID number (the order it appears in the ASCII table), and that's shown in the table (the 'dec' collumn). A space, for instance, has a value of 32. Exclamation marks have a value of 33. Tab has a value of 9, and so on, and so forth.

The Range Operator

Can you see now how characters are actually not as 'solid' as we normally think of them? A is only one place away from B, and 26 or so away from Z. TAB only has a value of 9, whereas A has a value of 65. So TAB is worth less than A.

This is how Range Operators work. They use the ASCII table.

Examples - When and Where This is Useful

Checking Characters

I've been using this a lot in a recent application I'm building. It has to test every character in a string, checking whether it meets certain criteria.

For instance, using mid$() to retrieve one character from a string, and upper$() to temporarily turn it into uppercase, we can check if that character is a letter.

All we do is compare if that character IS GREATER OR EQUAL TO A and LOWER OR EQUAL TO Z

Any character matching those criteria must be a letter in the alphabet.

You can do other things. In my application, all spaces, newlines and tabs are stripped out of the string. We can do this by testing every character like this:

character IS LOWER THAN !

An Exclamation mark has a value of 33, and all characters above it are useful for displaying things (text, numbers, math characters, brackets, etc). Below 33, you get tabs (9), spaces (32), New Lines (13 and 10), and so on. This one comparison will automatically identify them.

In my application, it also needs more complex tests. When deleting spaces, it must NOT delete ones where the characters LEFT and RIGHT of it are BOTH ordinary letters (A to Z) or numbers (0 to 9).

This method can also be used to do those tests, resulting in just a few small events that are testing for literally hundreds of different characters.


Arranging and Ordering Characters

It doesn't only work for single characters. With these statements, you can create a list of strings in alphabetical order. You can test if one string is alphabetically higher or lower than another.


In Conclusion

Hopefully, this will help someone. I've found it useful, and it's something I didn't think of for nearly 10 years of clicking, lol. But maybe I'm just thick.