Guide to TCL scripting for Eggdrop 1.6

[ Previous ] [ Index ] [ Next ]
[ Text version ]


9. Working with strings

In this chapter you will learn how to break strings apart.

9.1 Retrieving a character from a string

This command works in exactly the same way as the lindex command, only the command is a bit different.
To retrieve characters from a string we use the command string index.
The syntax of a string index command is string index <string> <number>.

The <string> is the string from which the characters must be retrieved.
This can be anything from a string as in variable to a command.

The <number> is the number of the character you want to retrieve, where 0 is the first character, 1 the second character, 2 the third character and so on like in the other commands for lists except that it now goes per character instead of per object.

The string index command outputs a string that contains the charater you specified.
If you were to input a list which contains an object that is defined with braces, the braces will also be regarded as characters in the string and not as items to designate a start and stop of an object.
For example, string index "test {foo bar} temp" 5 would return "{" and not "f" or "foo bar".

9.2 Retrieving multiple characters from a string

This command works in exactly the same way as the lrange command, only the command is a bit different. To retrieve multiple characters from a string we use the command string range.
The syntax of a string range command is string range <string> <start number> <end number>.

The <string> is the string from which the characters must be retrieved.
This can be anything from a string as in variable to a command.

The <start number> is the number from which character you want to start cutting, where 0 is again the first character 1 the second character and so on.

The <end number> is the number up until which character you want to cut, where 0 is the first character and so on like in the above commands.
Here you can use "end" aswell to designate that there must be cut until the end of the string.

The string range command outputs a string that consists of the cut text.
If you were to input a list which contains an object that is defined with braces, the braces will also be regarded as characters in the string and not as items to designate a start and top of an object. For example, string range "test {foo bar} temp" 5 8 would return "{foo" and not "foo ".

9.3 Determining how many characters a string has

This can be done with the string length command.
The syntax of a string length command is string length <string>.

The <string> is the string from which the characters must be retrieved.
This can be anything from a string as in variable to a command.

The string length command outputs a string that contains a number which equals the amount of characters the string has.
If you were to input a list which contains an object that is defined with braces, the braces will also be regarded as characters in the string and not as items to designate a start and top of an object. For example, string length "test {foo bar} temp" would return 19 including the braces and not 17.

9.4 Matchting strings against each other

At some points you might want to know if a certain piece of text is in a string. You can find out if it is or is not with the string match command.
The syntax of a string match command is string match <pattern> <string>.

The <pattern> is what you want to look for in the string.
Besides normal characters you can also put an asterix (*) or question mark in the pattern.
An asterix means that anything is matched and a question mark only one character.
For example if you have "*foo*" as <pattern> string match would find a match if it finds "foo" anywhere in the string, so if it was looking for "*foo*" in "test foobar" it would give a match. If you were to use "?foo*" it would find a match only if the string has the characters "foo" on the second through fourth place regardless of any suffixes, so if it was looking for "?foo*" in "test foobar" it would not match but "*foobar" would match.

The <string> is the string in which string match will look for the <pattern>.
This can be anything from a variable string to a command that outputs a string.

The string match outputs a 1 if it finds a match and 0 if it doesn't.

9.5 Changing the string case

A lot of things in TCL are case sensitive.
For example in most cases "FOO" would not be the same as "foo", so in some cases it might be wise to first convert something to lower case or upper case alone before matching it against something. This can be done with string tolower and string toupper.
The syntax of a string <tolower/toupper> command is string <tolower/toupper> <string>.

The <tolower/toupper> is to which you want to convert the case, tolower will convert your string to lower case and toupper will convert it to upper case.

The <string> is the string what you want to convert.
This can be anything from a variable string to a command that outputs a string.

The string <tolower/toupper> command outputs a string in which all the characters are converted to lower or upper case.
If you were to input a list which contains an object that is defined with braces, the braces will also be regarded as characters in the string and not as items to designate a start and top of an object.
For example, string tolower "test {foo BAR} temp" would return "test {foo bar} temp" and not "test foo bar temp".

9.6 Converting lists into strings

You can convert a list into a string with the join command.
The syntax of a join command is join <list> [character].

The <list> is the list you want to convert into a string, which can be anything from a string containing a list or a command that outputs a list for example.

The [character] is the character that will be put between the former list objects in the new string.
If no character is provided a space is put between the objects.

The join command returns a string with all the objects in the <list> seperated by the [character].
Any braces that are in the <list> which are used to define the start and end of a object will not be included in the new string, only the objects are transformed into a string.

A small example: join [list "test" {foo bar} "temp"] would result in a string containing "test foo bar temp" or join [list "test" {foo bar} "temp"] . would result in "test.foo bar.temp" (notice  that the space in "foo bar" has NOT been replaced by a dot because the join command joins the objects of a list and not the words!).


[ Text version ]
[ Previous ] [ Index ] [ Next ]


Design & Graphics by Shawn Borton of CalcioStar.com