![]() |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In a gaming environment based on text, it's natural to expect that we've
gone into a bit of trouble in making string handling functions both easy
to use and versatile. As you already know, strings can be added together
using the +
operator, even mixing in integers without any special
considerations. Floats and object pointers have to be converted however,
floats with the special ftoa()
efun (described later) and object
pointers with the file_name()
efun that I described earlier.
One of the most interesting properties of strings, apart from what they
contain, is the length. You find that with the efun strlen()
.
Since it accepts ints as well (returning 0 for them) you can use it
to test uninitialized string variables as well.
int strlen(string str) e.g. string str = "Fatty is a bloated blimp"; write("The length of the string '" + str + "' is " + strlen(str) + " characters.\n"); |
Often you will want to capitalize names and sentences for output to the
screen. You do that with the efun capitalize()
, it will only turn
the first character in the string to upper case. The converse of this
function is the efun lower_case()
, however, it turns the
entire string into lowercase and not only the first character.
string capitalize(string str) string lower_case(string str) e.g. void // Present a given name on the output, formatted properly present_nice_name(string name) { string new_name; // Assume name = "fAttY" new_name = lower_case(name); // Name is now = "fatty" new_name = capitalize(name); write("The name is: " + name + "\n"); /* The result is: The name is: Fatty */ } |
Sometimes it's desirable to break up a string in smaller pieces, just to
present a nicer output. The efun break_string()
will do that for
you. It can even pad spaces in front of the broken strings if you want that.
What it does is simply to insert newlines after whole words where you have
indicated you want to break it up. The third argument specifying either
space pad length or a string to pad with, is optional.
string break_string(string str, int brlen, int indlen|string indstr|void) e.g. string str = "This is the string I want to present in different ways."; write(break_string(str, 20) + "\n"); write(break_string(str, 20, 5) + "\n"); write(break_string(str, 20, "Fatty says: ") + "\n"); /* The result is: This is the string I want to present in different ways. This is the string I want to present in different ways. Fatty says: This is the string I Fatty says: want to present in Fatty says: different ways. */ |
You will very often want to present information stored in variables. As
shown you can do that by converting the contents to strings and then just
print the strings. Integers don't even have to be converted, you just add
them on with the +
-operator. However, what you get then is something
that's not very well formatted, you'll have to do that yourself. Particularly
if you try to produce tables this is a nuisance, having to determine the
length of strings and add on a certain amount of spaces depending on this
length and so on. Instead of doing this you can use the efun sprintf()
.
What sprintf()
does is simply to take a format-string that
describes how you want the resulting string to look and put in the contents
of the given variables according to your specifications. The result is
a string that you then can present on the screen with write()
for
example.
All characters in the format string will be copied to the resulting string with exceptions of the special pattern %<width spec><type spec>. The width specifier can contain a field width parameter, simply an integer that specifies the width of the box you want to put it in and if you want it left- or right-aligned. A positive number denotes right-alined insertion and negative number left-aligned. If you omit the width specifier the variable will be inserted in a box exactly the width of its contents. The type specifier is one or more characters defining what kind of variable you want to have inserted.
string str; int a; a = 7; str = sprintf("test: >%-3d%i<", 1, a); write(str + "\n"); // The result is: // test: >1 7< |
e.g.
write(sprintf("1:%d 2:%s 3:%c 4:%o\n5:%x 6:%X 7:%O\n", 5, "hupp happ", 85, 584, 32434, 85852, strlen)); // The result is: // 1:5 2:hupp happ 3:U 4:1110 // 5:7eb2 6:14F5C 7:<<FUNCTION &strlen()>> |
This specifier is also the only one you can use for denoting floats right now.
Now, these were all the available type specifiers with a few width specifiers given as examples. However, there's a lot more of them.
\n
-separated words
in a table within the field size. This naturally only is meaningful with
strings.
write((sprintf(">%19|s<\n", "Fatty the blimp"))); // The result is: // > Fatty the blimp < |
Very often you want to find out if a certain substring is part of a
greater string. You're not interested in exactly where the string
is, just that it is there. For that purpose you want something
that closely resembles the UNIX shell approach to string matching.
The efun wildmatch()
will do this for you. It simply
return 1 if a specified substring is part of a specified main
string, and 0 otherwise. The substring can contain the simple
UNIX pattern matching symbols.
int wildmatch(string pattern, string str); e.g. // Anything ending with .foo wildmatch("*.foo", "bar.foo") == 1 // Anything starting with a, b or c, containing at least // one more character wildmatch("[abc]?*", "axy") == 1 wildmatch("[abc]?*", "dxy") == 0 wildmatch("[abc]?*", "a") == 0 |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |