Previous
Hello World 
Next
Lists 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
String Manipulation

String Manipulation

After making mathematical scripts, let us move to strings. Strings, as the name implies, is a string of characters. Now let us try to make some scripts to make ourselves familiar with the use of strings.

Assigning a string to a variable is as easy as assigning a number to a variable.

set str "This is a string"

You can put some special characters in it to format the output.

set str "Tcl/tk\nis\na\ngood\nlanguage"

This will give the variable 'str' the below text

Tcl/tk
is
a
good
language

The '\n' means new line. There are other substitutions also. For more details read the manual. The strings variables can be used very easily. Consider the following script.

set joke1 "There are two ways to write error-free programs. Only the third one works."
set joke2 "A bug in the code is worth two in the documentation."

label .jokes -text "Joke 1 : $joke1 \nJoke 2 : $joke2"
pack .jokes

Another way of assigning a string is with the '{...}' (braces or curly brackets). An example is in order.

set price {$20}

The braces make sure that no substitutions are done inside it. If we do the script not using the braces but using the " (double quotes), set price "$20", it will show an error saying that Tcl can't read the variable called 20. Substitutions can take place in the double quotes. So tcl thinks that 20 is a variable as it follows a $ sign. This won't happen inside a braces. This was explained before too.

Some Basic String Manipulation Techniques

The 'string' function is the basis for almost all string manipulation techniques. Some of the most used functions are given below. Please note that in all the cases, the result is returned. To get the result into a variable the following code must be used:
set result [string index "Binny" 0]
Examples are provided for a few.

1. Get the n'th letter from a string.
[string index <string> <character index>]

Example:
set quest "What is at the beginning of the end and at the end of all time?"
set ans [string index $quest 32]
label .question -text "Question : $quest"
label .answer -text "Answer : \"$ans\" OR \"[string index $quest end-1]\""
pack .question
pack .answer

2. Get string from character number 'a' to character number 'b'
[string range <string> <first character index> <last character index>]

Example:
set common "marriage, a man"
set end "for the women he loves"
set verb "yearns"

label .before -text "Before...\nBefore $common $verb $end\n"
label .afterm -text "After...\nAfter $common [string range $verb 1 end] $end"

pack .before
pack .afterm

3. Get first and last instance of a character in a string.
[string first <string> <character> ?startIndex?] - For getting the first instance.
[string last <string> <character> ?startIndex?] - For getting the last instance.

4. Get length of a string
[string length <string>]

There are many other string functions. See manual for a complete list.

Previous
Hello World 
Next
Lists 
blog comments powered by Disqus
Subscribe to Feed