Contents Up Previous Next

String functions

GetMessageText
StrCat
StrCaseComp
StrComp
StrContains
StrCopy
StrFormat
StrGetCharAt
StringToInt
StrLen
StrSetCharAt
StrToLowerCase
StrToUpperCase


GetMessageText

GetMessageText (int message, string buffer)
Gets the text of global or local message number MESSAGE into the string BUFFER. This is useful if you want to store, for example, a room description in Message 1 in each room, this function will display that message from the current room.

If an invalid message number is supplied, BUFFER will be set to an empty string.

NOTE: Since script strings have a limit of 200 characters, whereas system messages do not, you will only get the first 200 characters of a message with this function.

Example:

string buffer;
GetMessageText (501, buffer);
Display ("Global message 501 is: %s", buffer);
will print the contents of global message 501.


StrCat

StrCat (string str1, string str2)
Appends STR2 to the end of STR1.

Example:

string name;
StrCopy(name,"My name is ");
StrCat (name,"Chris"); 
will result a string name which will be "My name is Chris".


StrCaseComp

StrCaseComp (string str1, string str2)
Does a case-insensitive comparison of STR1 and STR2. Using this function, "Dog", "dog" and "doG" will all be considered equal.

Returns 0 if the two strings match, non-zero otherwise.

Example:

string input;
if (StrCaseComp("password",input)==0)
    { code here }
will execute the code if the input string (usually typed by the player) is "password" (no case sensitive).

See Also: StrComp, StrContains


StrComp

StrComp (string str1, string str2)
Compares the strings STR1 and STR2. Returns zero if they are identical, and non-zero if they are not.

Example:

string input;
if (StrComp("password",input)==0)
    { code here }
will execute the code if the input string (usually typed by the player) is "password" (case sensitive).

See Also: StrCaseComp, StrContains


StrContains

StrContains (string haystack, string needle)
Checks to see if NEEDLE is contained within HAYSTACK. Returns the character position of the match if it is, or -1 if it is not.

This function is not case sensitive - ie. testing "test string" for "sTRiN" would match.

Example:

int result = StrContains ("The haystack had a needle in it somewhere.", "a needle");

if (result == -1) {
  Display("The string didn't contain the needle.");
}
else {
  Display("a needle was found starting at character %d in the string.", result);
}
See Also: StrCaseComp, StrLen, StrSetCharAt


StrCopy

StrCopy (string str1, string str2)
Copies the contents of STR2 into STR1, overwriting STR1's original contents. Use this instead of the assignment STR1=STR2 .

Example:

string message;
StrCopy(message,"This is a message"); 
will result a string message that will be "This is a message"


StrFormat

StrFormat (string destination, string fmt, ...)
Processes the string FMT in the same way as the Display function does but instead of displaying it on the screen, puts the result into DESTINATION.

You can insert the value of variables into the message. For more information, see the string formatting section.

NOTE: You cannot use the DESTINATION string as one of the format arguments, or the results will be unpredictable. (ie. StrFormat(buffer, "%s", buffer); won't work)

Example:

int health=10;
string buffer;
StrFormat (buffer, "%d", health) 
will fill buffer with the string representation of the value of health.

See Also: Display


StrGetCharAt

StrGetCharAt (string text, int position)
Returns the character at POSITION within the string TEXT.

POSITION is the character index (where 0 is the first character, and the last allowable value is StrLen - 1).

If POSITION is outside the string, this function returns 0.

See Also: StrLen, StrSetCharAt


StringToInt

StringToInt (string str1)
Converts the string STR1 into an integer, and returns that value. Returns zero if the string does not contain a number.

Example:

int number1,number2;
number1=StringToInt("53");
number2=StringToInt("hello");
will set number1 value to 53 and number2 value to 0. This function is useful for processing strings input from the user.

NOTE: To convert an integer to a string, you can use the StrFormat command.

See Also: InputBox, StrFormat


StrLen

StrLen (string str1)
Returns the length, in characters, of the string STR1.

Example:

int length;
length=StrLen("adventure");
will set the value of the integer length to 9.


StrSetCharAt

StrSetCharAt(string text, int position, int character)
Changes the character at POSITION in string TEXT to CHARACTER.

POSITION is the character index (where 0 is the first character, and the last allowable value is StrLen - 1).

string temp;
StrCopy (temp, "Hello");
StrSetCharAt (temp, 3, 'm');
will change the string to contain "Helmo".

You can also use this to truncate a string, by passing 0 as the character. For example,

string temp;
StrCopy (temp, "Hello");
StrSetCharAt (temp, 2, 0);
will change the string to read "He".

Finally, you can append a character to the string, by passing POSITION as one after the end of the string:

StrSetCharAt(mystring, StrLen(mystring), '!');

will append a ! to the end of the string.

See Also: StrGetCharAt, StrLen


StrToLowerCase

StrToLowerCase (string str1)
Converts the string STR1 to lower case.

(Lower case means converting all capital letters in the string to equivalent non-capital letters. Any other characters, such as numerical digits, are not affected).

Example:

string input;
InputBox ("Enter some text:", input);
StrToLowerCase (input);
Display ("Your text, in lower case, is '%s'.", input);
reads a string from the user, and displays it back to them in lower case.

See Also: StrToUpperCase


StrToUpperCase

StrToUpperCase (string str1)
Converts the string STR1 to upper case.

(Upper case means converting all small letters in the string to equivalent capital letters. Any other characters, such as numerical digits, are not affected).

Example:

string input;
InputBox ("Enter some text:", input);
StrToUpperCase (input);
Display ("Your text, in upper case, is '%s'.", input);
reads a string from the user, and displays it back to them in upper case.

See Also: StrToLowerCase

Browser Based Help. Published by chm2web software.
1